📑 Qualification Round 2022
Task 1. Punched Cards
📓 Variant 1. Python Only
T=int(input())
for t in range(T):
N=input()
print('Case #{}:'.format(t+1))
[R,C]=[int(n) for n in N.split()]
str1='..+-+'+(C-2)*'-+'
str2='..|.|'+(C-2)*'.|'
str3='+-+-+'+(C-2)*'-+'
str4='|.|.|'+(C-2)*'.|'
[print(el) for el in [str1,str2,str3,str4,str3]]
for i in range(R-2):
print(str4)
print(str3)
📓 Variant 1. SageMath Interactive
📓 Variant 2. Python Only
def gen_table(r,c):
for i in range(2*r+1):
for j in range(2*c+1):
if (i < 2 and j < 2):
yield '.'
else:
el=((j+1)%2)*((i+1)%2)*'+'+\
(j%2)*((i+1)%2)*'-'+\
((j+1)%2)*(i%2)*'|'+\
(j%2)*(i%2)*'.'
yield el
yield '\n'
T=int(input())
for t in range(T):
N=input()
[R,C]=[int(n) for n in N.split()]
gen=gen_table(R,C)
print('Case #{}:'.format(t+1))
print(''.join(gen))
📓 Variant 2. SageMath Interactive
Task 2. 3D Printing
📓 Variant 1. Python Only
T=int(input())
for t in range(T):
C,M,Y,K=[],[],[],[]
for i in range(3):
N=input()
N=[int(n) for n in N.split()]
C+=[N[0]]; M+=[N[1]]; Y+=[N[2]]; K+=[N[3]]
c,m,y,k=min(C),min(M),min(Y),min(K)
if c+m+y+k < 10**6:
R='IMPOSSIBLE'
else:
m=min([m,10**6-c])
y=min([y,10**6-c-m])
k=10**6-c-m-y
R='{} {} {} {}'.format(c,m,y,k)
print('Case #{}: {}'.format(t+1,R))
📓 Variant 1. SageMath Interactive
📓 Variant 2. Python Only
def gen_vector(v,s=10**6):
if sum(v) < s:
yield 'IMPOSSIBLE'
else:
for c in range(v[0],1,-1):
m=min([v[1],10**6-c])
y=min([v[2],10**6-c-m])
k=10**6-c-m-y
yield '%d %d %d %d'%(c,m,y,k)
T=int(input())
for t in range(T):
N=[]
for i in range(3):
N.append(list(map(int,input().split())))
v=[min([N[i][j] for i in range(3)])
for j in range(4)]
print('Case #%d: %s'%(t+1,next(gen_vector(v))))
📓 Variant 2. SageMath Interactive
Task 3. d1000000
📓 Variant 1. Python Only
T=int(input())
for t in range(T):
N=int(input())
S=input()
k=N
if k>4:
S=sorted([int(s) for s in S.split()])[::-1]
k=N=min([N,S[0]])
for i in range(N):
if S[i] < N-i:
k-=1
print('Case #{}: {}'.format(t+1,k))
📓 Variant 1. SageMath Interactive
Task 4. Chain Reactions
📓 Variant 1. Python Only
class DGraph:
def __init__(self,n,f,p):
self.N=n
self.P=p
self.F=f
self.G={k:0 for k in range(n)}
self.D={k:0 for k in range(n)}
def add_edges(self):
for i,el in enumerate(self.P):
self.G[i]=el-1
if el!=0:
self.D[el-1]+=1
def max_fun(self):
indegree=self.D
inits=[]
for i in range(self.N):
if indegree[i]==0:
inits.append(i)
fmin=[10**12]*self.N
fsum=sum([self.F[i] for i in inits])
while inits:
i=inits.pop(0)
node=self.G[i]
if node!=-1:
indegree[node]-=1
fmin[node]=min([fmin[node],self.F[i]])
if indegree[node]==0:
inits.append(node)
self.F[node]=max([self.F[node],fmin[node]])
fsum+=self.F[node]-fmin[node]
return fsum
T=int(input())
for t in range(T):
N=int(input())
F=list(map(int,input().split()))
P=list(map(int,input().split()))
dgraph=DGraph(N,F,P)
dgraph.add_edges()
FSUM=dgraph.max_fun()
print('Case #{}: {}'.format(t+1,FSUM))
📓 Variant 1. SageMath Interactive
Task 5. Twisty Little Passages
📓 Variant 1. Python Only
📓 Variant 1. SageMath Interactive