QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#319486 | #5583. Color Tubes | chmpro | RE | 10ms | 9584kb | Python3 | 1.8kb | 2024-02-02 17:10:09 | 2024-02-02 17:10:09 |
Judging History
answer
import sys
input=sys.stdin.readline
F=lambda:[*map(int,input().split())]
def move(i,j):
if i==j:return
ANS.append((i,j))
h1=2
while A[i][h1]==0:h1-=1
x=A[i][h1]
A[i][h1]=0
if h1>=1:CNT[x]-=1
idx1=0
while POS[x][idx1]!=(i,h1):idx1+=1
h2=0
while A[j][h2]>0:h2+=1
A[j][h2]=x
if h2>=1:CNT[x]+=1
POS[x][idx1]=(j,h2)
idx2=0
while POS[0][idx2]!=(j,h2):idx2+=1
POS[0][idx2]=(i,h1)
def make_empty():
p0=POS[0][0][0]
for idx in range(1,3):
if POS[0][idx][0]!=p0:move(p0,POS[0][idx][0])
N=int(input())
A=[[]]+[F()for _ in range(N+1)]
CNT=[0]*(N+1)
POS=[[]for _ in range(N+1)]
for n in range(1,N+2):
for h in range(3):
cur=A[n][h]
if h>=1:CNT[cur]+=1
POS[cur].append((n,h))
ANS=[]
NXT=[i+1 for i in range(N+1)];NXT[N]=1
PRV=[i-1 for i in range(N+1)];PRV[1]=N
cur=1
for _ in range(N):
make_empty()
#print(ANS,CNT,POS)
pos_0=POS[0][0][0]
while 1:
if CNT[cur]<=1:cur=NXT[cur];continue
idx=0
while idx<3 and POS[cur][idx][1]<2:idx+=1
if idx==3:cur=NXT[cur];continue
else:break
#print(ANS)
POS[cur].sort(key=lambda x:-x[1])
p1=POS[cur][0][0]
move(p1,pos_0)
p2=POS[cur][1][0];h=POS[cur][1][1]
chk=1
if h==2 or p1==p2:move(p2,pos_0)
else:
move(p2,p1)
chk=0 #p1에 자리 없음
move(p2,pos_0)
p3=POS[cur][2][0];h=POS[cur][2][1]
if h==2 or (h==1 and (p3==p2)) or (h==0 and p3==p2==p1):move(p3,pos_0)
elif h==1:
#print(ANS)
move(p3,p2)
move(p3,pos_0)
else:
if chk:
move(p3,p1)
move(p3,p2)
move(p3,pos_0)
else:
move(p3,p2)
move(p3,p2)
move(p3,pos_0)
nxt=NXT[cur]
prv=PRV[cur]
NXT[prv]=nxt
PRV[nxt]=prv
cur=nxt
print(len(ANS))
for ans in ANS:print(*ans)
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 10ms
memory: 9452kb
input:
3 2 2 0 1 3 1 3 1 2 3 0 0
output:
17 1 4 1 4 2 1 3 2 3 1 2 3 2 3 2 1 4 2 4 2 3 4 3 2 4 3 4 3 3 4 3 4 3 4
result:
ok correct
Test #2:
score: 0
Accepted
time: 9ms
memory: 9488kb
input:
1 0 0 0 1 1 1
output:
3 2 1 2 1 2 1
result:
ok correct
Test #3:
score: 0
Accepted
time: 7ms
memory: 9408kb
input:
2 2 1 0 2 1 0 2 1 0
output:
10 1 2 1 3 2 1 2 1 3 2 3 1 3 2 2 3 2 3 2 3
result:
ok correct
Test #4:
score: 0
Accepted
time: 9ms
memory: 9576kb
input:
3 0 0 0 1 2 3 1 2 3 1 2 3
output:
13 2 1 3 1 4 1 2 3 2 4 3 2 3 2 4 3 4 2 4 3 3 4 3 4 3 4
result:
ok correct
Test #5:
score: 0
Accepted
time: 9ms
memory: 9532kb
input:
3 1 2 3 1 2 3 0 0 0 1 2 3
output:
13 1 3 2 3 4 3 1 2 1 4 2 1 2 1 4 2 4 1 4 2 2 4 2 4 2 4
result:
ok correct
Test #6:
score: 0
Accepted
time: 2ms
memory: 9472kb
input:
3 1 2 3 1 2 3 1 2 3 0 0 0
output:
13 1 4 2 4 3 4 1 2 1 3 2 1 2 1 3 2 3 1 3 2 2 3 2 3 2 3
result:
ok correct
Test #7:
score: 0
Accepted
time: 9ms
memory: 9532kb
input:
6 0 0 0 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6 4 5 6
output:
27 2 1 3 1 4 1 2 3 2 4 5 2 6 2 7 2 7 6 7 5 3 7 3 7 4 3 4 7 4 3 6 4 5 6 5 4 6 5 6 4 6 5 3 6 3 6 3 6 5 3 5 3 5 3
result:
ok correct
Test #8:
score: 0
Accepted
time: 9ms
memory: 9540kb
input:
6 0 0 0 1 5 6 1 2 6 1 2 3 4 2 3 4 5 3 4 5 6
output:
29 4 1 5 1 6 1 4 5 4 6 2 4 3 4 7 4 7 3 7 2 5 7 3 5 3 7 5 3 5 7 5 3 3 5 2 5 6 3 6 2 6 5 2 6 2 6 3 2 3 6 3 2 2 3 2 3 2 3
result:
ok correct
Test #9:
score: 0
Accepted
time: 9ms
memory: 9584kb
input:
9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0 0 0
output:
41 1 10 4 10 7 10 1 4 1 7 2 1 5 1 8 1 8 5 8 2 3 8 6 8 9 8 9 6 9 3 4 9 4 9 7 4 7 9 7 4 5 7 2 5 2 7 5 2 5 7 5 2 6 5 3 6 3 5 6 3 6 5 6 3 4 6 4 6 4 6 2 4 2 4 2 4 3 2 3 2 3 2
result:
ok correct
Test #10:
score: 0
Accepted
time: 4ms
memory: 9404kb
input:
10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 0 0 0
output:
52 7 11 4 7 4 11 1 4 1 4 1 11 4 1 7 1 8 4 8 7 8 1 7 8 4 7 4 8 5 4 5 4 5 8 4 5 7 5 2 4 2 7 2 5 7 2 4 7 4 2 9 4 9 4 9 2 7 9 4 9 6 7 6 4 6 9 4 6 4 6 3 4 3 4 3 6 4 3 7 3 10 4 10 7 10 3 7 10 4 7 4 10 7 4 7 4 7 10 4 7 4 7 4 7
result:
ok correct
Test #11:
score: 0
Accepted
time: 2ms
memory: 9468kb
input:
11 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 0 0 0
output:
55 4 12 8 4 8 12 1 8 1 8 1 12 8 1 4 1 5 8 5 4 5 1 4 5 8 4 8 5 9 8 9 8 9 5 4 9 8 9 2 4 2 8 2 9 8 2 8 2 6 8 6 8 6 2 4 6 8 6 10 4 10 8 10 6 8 10 8 10 3 8 3 8 3 10 8 3 4 3 7 8 7 4 7 3 4 7 8 4 8 7 11 8 11 8 11 7 4 11 8 11 4 8 4 11 8 4 8 4 8 4
result:
ok correct
Test #12:
score: 0
Accepted
time: 6ms
memory: 9504kb
input:
12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 0 0 0
output:
55 1 13 5 13 9 13 1 5 1 9 2 1 6 1 10 1 10 6 10 2 3 10 7 10 11 10 11 7 11 3 4 11 8 11 12 11 12 8 12 4 5 12 5 12 9 5 9 12 9 5 6 9 2 6 2 9 6 2 6 9 6 2 7 6 3 7 3 6 7 3 7 6 7 3 8 7 4 8 4 7 8 4 8 7 8 4 5 8 5 8 5 8 2 5 2 5 2 5 3 2 3 2 3 2 4 3 4 3 4 3
result:
ok correct
Test #13:
score: 0
Accepted
time: 2ms
memory: 9584kb
input:
13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 0 0 0
output:
67 9 14 5 9 5 14 1 5 1 5 1 14 5 1 9 1 10 5 10 9 10 1 9 10 5 9 5 10 6 5 6 5 6 10 5 6 9 6 2 5 2 9 2 6 9 2 5 9 5 2 11 5 11 5 11 2 9 11 5 11 7 9 7 5 7 11 5 7 5 7 3 5 3 5 3 7 5 3 9 3 12 5 12 9 12 3 9 12 5 9 5 12 8 5 8 5 8 12 5 8 9 8 4 5 4 9 4 8 9 4 5 9 5 4 13 5 13 5 13 4 9 13 5 13 9 5 9 13 5 9 5 9 5 9
result:
ok correct
Test #14:
score: 0
Accepted
time: 6ms
memory: 9460kb
input:
9 1 7 3 6 1 7 5 2 0 2 5 0 3 7 8 2 4 6 9 4 9 8 1 0 6 3 5 4 8 9
output:
50 3 4 3 8 1 3 9 1 9 3 5 9 5 9 5 3 8 5 1 5 4 1 4 5 8 4 8 4 2 8 9 8 1 9 1 8 1 2 4 1 9 4 9 1 10 9 10 1 10 9 7 10 9 7 9 10 7 9 7 9 7 10 2 7 2 7 4 2 4 7 2 4 2 4 9 2 6 9 6 2 9 6 9 2 9 6 4 9 6 9 6 9 6 4 4 6 4 6 4 6
result:
ok correct
Test #15:
score: -100
Dangerous Syscalls
input:
10 3 3 8 5 6 9 1 2 3 7 7 10 9 5 0 7 8 0 5 1 4 1 6 10 4 2 0 8 9 10 2 6 4