Submission #6362007
Source Code Expand
import sys
input = sys.stdin.readline
import itertools
N = int(input())
# 各index i と n に対して、左側にn以下のblack/whiteがいくつあるかを数える
# とりあえず1を入れて、あとでcumsum
B_pos = [0] * (N+1)
W_pos = [0] * (N+1)
B_cnt = [[0] * (N+1) for _ in range(2*N)]
W_cnt = [[0] * (N+1) for _ in range(2*N)]
for i in range(2*N):
c,a = input().split()
a = int(a) # 0-index化
if c == 'B':
B_pos[a] = i
else:
W_pos[a] = i
for i in range(1,N+1):
B_cnt[B_pos[i]][i] += 1
W_cnt[W_pos[i]][i] += 1
# cumsum
for i in range(1,2*N):
for j in range(N+1):
B_cnt[i][j] += B_cnt[i-1][j]
W_cnt[i][j] += W_cnt[i-1][j]
for i in range(2*N):
for j in range(1,N+1):
B_cnt[i][j] += B_cnt[i][j-1]
W_cnt[i][j] += W_cnt[i][j-1]
B_cnt
W_cnt
# Bi,Wj まで並べるためのコスト
dp = [[0] * (N+1) for _ in range(N+1)]
INF = 1 << 50
for b in range(N+1):
rng_w = range(N+1) if b else range(1,N+1)
for w in rng_w:
p = B_pos[b]
q = W_pos[w]
x1,x2 = INF,INF
if b >= 1:
x1 = dp[b-1][w] + (p - B_cnt[p][b-1] - W_cnt[p][w])
if w >= 1:
x2 = dp[b][w-1] + (q - W_cnt[q][w-1] - B_cnt[q][b])
dp[b][w] = x1 if x1 < x2 else x2
answer = dp[N][N]
print(answer)
Submission Info
| Submission Time | |
|---|---|
| Task | E - Sorted and Sorted |
| User | maspy |
| Language | PyPy3 (2.4.0) |
| Score | 600 |
| Code Size | 1381 Byte |
| Status | AC |
| Exec Time | 1182 ms |
| Memory | 194056 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 600 / 600 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 0_000.txt, 0_001.txt, 0_002.txt |
| All | 0_000.txt, 0_001.txt, 0_002.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 1_033.txt, 1_034.txt, 1_035.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 0_000.txt | AC | 170 ms | 38256 KiB |
| 0_001.txt | AC | 165 ms | 38256 KiB |
| 0_002.txt | AC | 167 ms | 38256 KiB |
| 1_003.txt | AC | 166 ms | 38256 KiB |
| 1_004.txt | AC | 173 ms | 38256 KiB |
| 1_005.txt | AC | 167 ms | 38256 KiB |
| 1_006.txt | AC | 165 ms | 38256 KiB |
| 1_007.txt | AC | 168 ms | 38256 KiB |
| 1_008.txt | AC | 168 ms | 38256 KiB |
| 1_009.txt | AC | 162 ms | 38256 KiB |
| 1_010.txt | AC | 167 ms | 39864 KiB |
| 1_011.txt | AC | 165 ms | 38256 KiB |
| 1_012.txt | AC | 164 ms | 38256 KiB |
| 1_013.txt | AC | 165 ms | 38256 KiB |
| 1_014.txt | AC | 955 ms | 158216 KiB |
| 1_015.txt | AC | 301 ms | 61420 KiB |
| 1_016.txt | AC | 748 ms | 127708 KiB |
| 1_017.txt | AC | 417 ms | 80604 KiB |
| 1_018.txt | AC | 186 ms | 40304 KiB |
| 1_019.txt | AC | 261 ms | 54512 KiB |
| 1_020.txt | AC | 847 ms | 142088 KiB |
| 1_021.txt | AC | 395 ms | 84956 KiB |
| 1_022.txt | AC | 297 ms | 64220 KiB |
| 1_023.txt | AC | 675 ms | 128476 KiB |
| 1_024.txt | AC | 962 ms | 186248 KiB |
| 1_025.txt | AC | 1182 ms | 192136 KiB |
| 1_026.txt | AC | 1163 ms | 192008 KiB |
| 1_027.txt | AC | 1167 ms | 192008 KiB |
| 1_028.txt | AC | 1170 ms | 192136 KiB |
| 1_029.txt | AC | 1173 ms | 192008 KiB |
| 1_030.txt | AC | 1160 ms | 192008 KiB |
| 1_031.txt | AC | 1167 ms | 192136 KiB |
| 1_032.txt | AC | 940 ms | 192264 KiB |
| 1_033.txt | AC | 930 ms | 192264 KiB |
| 1_034.txt | AC | 1001 ms | 192008 KiB |
| 1_035.txt | AC | 991 ms | 194056 KiB |