Please sign in first.
Submission #13539757
Source Code Expand
import sys
import numpy as np
from numba import njit
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
@njit('(i4,i4[::1])', cache=True)
def solve(N, P):
W = N + 2
dp = np.zeros((W, W), np.int32)
for _ in range(4):
dp = dp.T[::-1]
for n in range(1, (N + 1) // 2 + 1):
dp[n, n:-n] = n
filled = np.zeros_like(dp)
filled[1:-1, 1:-1] = 1
dp = dp.ravel()
filled = filled.ravel()
stack = np.empty_like(dp)
ans = 0
for n in P:
n -= 1
h, w = divmod(n, N)
v = (h + 1) * W + (w + 1)
ans += dp[v] - 1
filled[v] = 0
k = min(dp[v - 1], dp[v + 1], dp[v - W], dp[v + W])
if dp[v] == k:
return ans
p = 0
stack[0] = v
dp[v] = k
while p >= 0:
v = stack[p]
p -= 1
for dx in (-1, 1, -W, W):
w = v + dx
k = dp[v] + filled[w]
if dp[w] > k:
dp[w] = k
p += 1
stack[p] = w
return ans
N = int(readline())
P = np.array(read().split(), np.int32)
print(solve(N, P))
Submission Info
| Submission Time | |
|---|---|
| Task | B - Joker |
| User | maspy |
| Language | Python (3.8.2) |
| Score | 700 |
| Code Size | 1260 Byte |
| Status | AC |
| Exec Time | 782 ms |
| Memory | 123544 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 700 / 700 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example0.txt, example1.txt, example2.txt |
| All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, example0.txt, example1.txt, example2.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 000.txt | AC | 506 ms | 107276 KiB |
| 001.txt | AC | 514 ms | 109232 KiB |
| 002.txt | AC | 715 ms | 122812 KiB |
| 003.txt | AC | 489 ms | 106080 KiB |
| 004.txt | AC | 475 ms | 106056 KiB |
| 005.txt | AC | 472 ms | 105684 KiB |
| 006.txt | AC | 472 ms | 106360 KiB |
| 007.txt | AC | 501 ms | 106052 KiB |
| 008.txt | AC | 487 ms | 106152 KiB |
| 009.txt | AC | 488 ms | 106796 KiB |
| 010.txt | AC | 482 ms | 106300 KiB |
| 011.txt | AC | 489 ms | 106760 KiB |
| 012.txt | AC | 480 ms | 106088 KiB |
| 013.txt | AC | 489 ms | 106940 KiB |
| 014.txt | AC | 490 ms | 107000 KiB |
| 015.txt | AC | 514 ms | 109832 KiB |
| 016.txt | AC | 757 ms | 121708 KiB |
| 017.txt | AC | 755 ms | 121360 KiB |
| 018.txt | AC | 774 ms | 123544 KiB |
| 019.txt | AC | 768 ms | 121592 KiB |
| 020.txt | AC | 782 ms | 121600 KiB |
| example0.txt | AC | 470 ms | 106932 KiB |
| example1.txt | AC | 482 ms | 106900 KiB |
| example2.txt | AC | 466 ms | 106080 KiB |