結果
| 問題 | No.5026 Spectrum Router |
| コンテスト | |
| ユーザー |
Koi
|
| 提出日時 | 2026-11-23 23:08:46 |
| 言語 | PyPy3 (8.0.1 + ACL) |
| 結果 |
TLE
不安定
|
| 実行時間 | - |
| コード長 | 3,978 bytes |
| 記録 | |
| コンパイル時間 | 86 ms |
| コンパイル使用メモリ | 64,424 KB |
| 実行使用メモリ | 67,040 KB |
| スコア | 0 |
| 最終ジャッジ日時 | 2026-11-23 23:15:34 |
| 合計ジャッジ時間 | 15,559 ms |
|
ジャッジサーバーID (参考情報) |
judge4_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 6 TLE * 1 -- * 43 |
ソースコード
import sys,time,random
t0 = time.time()
N = int(input())
A = list(map(int, input().split()))
M = 4 * N
Ans = [[0] * N for _ in range(N)]
def calc_score(L):
cost = 0
for i in range(len(L)):
for j in range(i):
if(L[i] < L[j]):
cost += 1
return cost
Ins = [[[-1] * 4 for _ in range(N)] for _ in range(N)]
Outs = [[[-1] * 4 for _ in range(N)] for _ in range(N)]
Goals = [-1] * M
ToWhere = [[2, 3, 0, 1], [3,2, 1, 0], [1, 0, 3, 2]]
DX = [-1, 0, 1, 0]
DY = [0, 1, 0, -1]
BWhere = [[[-1] * 4 for _ in range(N)] for _ in range(N)]
for i in range(4 * N):
if(i < N):
sx, sy, d = 0, i, 0
elif(i < N * 2):
sx, sy, d = i - N, N - 1, 1
elif(i < N * 3):
sx, sy, d = N - 1, 3 * N - 1 - i, 2
else:
sx, sy, d = 4 * N - 1 - i,0,3
BWhere[sx][sy][d] = i
def calc_Ins_and_Outs():
for i in range(4 * N):
if(i < N):
sx, sy, d = 0, i, 0
elif(i < N * 2):
sx, sy, d = i - N, N - 1, 1
elif(i < N * 3):
sx, sy, d = N - 1, 3 * N - 1 - i, 2
else:
sx, sy, d = 4 * N - 1 - i,0,3
while True:
Ins[sx][sy][d] = i
nxt_d = ToWhere[Ans[sx][sy]][d]
Outs[sx][sy][nxt_d] = i
if(BWhere[sx][sy][nxt_d] != -1):
Goals[i] = BWhere[sx][sy][nxt_d]
break
sx += DX[nxt_d]
sy += DY[nxt_d]
d = nxt_d ^ 2
if(sx < 0 or sx >= N or sy < 0 or sy >= N):
break
# calc_Ins_and_Outs()
# print(Ins)
# print(Outs)
# print(Goals)
cnt = 0
while time.time() - t0 < 0.7:
cnt += 1
# print(cnt,file=sys.stderr)
calc_Ins_and_Outs()
Seen = set()
while len(Seen) < M // 3:
x = random.randint(0, N - 1)
y = random.randint(0, N - 1)
is_ok = True
for k in range(4):
if(Ins[x][y][k] in Seen):
is_ok = False
break
if(not is_ok):
continue
best_c = -1
best_cost = 10000
for c in range(3):
L = []
for k in range(4):
# 今はIns[x][y][k]->Goals[Outs[x][y][ToWhere[状態][k]]]
L.append((Goals[Outs[x][y][ToWhere[c][k]]], A[Ins[x][y][k]]))
L.sort()
cost = calc_score([l[1] for l in L])
if(cost < best_cost):
best_cost = cost
best_c = c
if(Ans[x][y] != best_c):
Ans[x][y] = best_c
for k in range(4):
Seen.add(Ins[x][y][k])
current_cost = calc_Ins_and_Outs()
while time.time() - t0 < 1.8:
cnt += 1
# print(cnt,file=sys.stderr)
calc_Ins_and_Outs()
Seen = set()
while len(Seen) < M // 3:
if(time.time() - t0 > 1.85):
break
x = random.randint(0, N - 1)
y = random.randint(0, N - 1)
is_ok = True
for k in range(4):
if(Ins[x][y][k] in Seen):
is_ok = False
break
if(not is_ok):
continue
best_c = -1
best_cost = 10000
L = [-1] * M
for c in range(3):
for i in range(M):
L[Goals[i]] = A[i]
for k in range(4):
# 今はIns[x][y][k]->Goals[Outs[x][y][ToWhere[状態][k]]]
L[Goals[Outs[x][y][ToWhere[c][k]]]] = A[Ins[x][y][k]]
cost = calc_score(L)
if(cost < best_cost):
best_cost = cost
best_c = c
if(Ans[x][y] != best_c):
Ans[x][y] = best_c
for k in range(4):
Seen.add(Ins[x][y][k])
for i in range(N):
print("".join([str(x) for x in Ans[i]]))
Koi