結果

問題 No.1029 JJOOII 3
ユーザー mkawa2mkawa2
提出日時 2021-04-16 17:55:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,492 bytes
コンパイル時間 90 ms
コンパイル使用メモリ 11,184 KB
実行使用メモリ 32,272 KB
最終ジャッジ日時 2023-09-15 15:33:28
合計ジャッジ時間 9,012 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
32,272 KB
testcase_01 AC 17 ms
8,568 KB
testcase_02 AC 17 ms
8,568 KB
testcase_03 AC 1,279 ms
10,824 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(10**6)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def FI(): return float(sys.stdin.buffer.readline())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LI1(): return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
dij = [(0, 1), (1, 0), (1, 1), (1, -1)]
inf = 10**16
# md = 998244353
md = 10**9+7

n, k = LI()
ss = []
cc = []
for _ in range(n):
    s, c = SI().split()
    ss.append(s)
    cc.append(int(c))

dpj = [0]+[inf]*k
dpo = [0]+[inf]*k
dpi = [0]+[inf]*k

def update(dp, cnt):
    for i in range(k+1):
        pi = max(0, i-cnt)
        dp[i] = min(dp[i], dp[pi]+cost)

for s, cost in zip(ss, cc):
    cj = co = ci = 0
    for c in s:
        if c == "J": cj += 1
        elif c == "O": co += 1
        elif c == "I": ci += 1
    update(dpj, cj)
    update(dpo, co)
    update(dpi, ci)

joi = [[s.count("J"), s.count("O"), s.count("I")]*3 for s in ss]
ans = dpj[k]+dpo[k]+dpi[k]
for i, (s, cost) in enumerate(zip(ss, cc)):
    m = len(s)
    cj = co = 0
    ci = joi[i][2]
    l = r = 0
    while r < m:
        while r < m and co < k:
            if s[r] == "O": co += 1
            elif s[r] == "I": ci -= 1
            r += 1
        if co < k: break
        while l < r and co == k:
            if s[l] == "J": cj += 1
            elif s[l] == "O": co -= 1
            l += 1
        cur = dpj[max(0, k-cj)]+cost+dpi[max(0, k-ci)]
        if cur < ans: ans = cur
        # print(s, l, r, cj, co, ci, ans)

for i, (s0, cost0) in enumerate(zip(ss, cc)):
    for j, (s1, cost1) in enumerate(zip(ss, cc)):

        cj, co0 = 0, joi[i][1]
        for c0 in s0:
            if c0 == "I": continue
            if c0 == "J": cj += 1
            if c0 == "O": co0 -= 1

            co1, ci = 0, joi[j][2]
            for c1 in s1:
                if c1 == "J": continue
                if c1 == "O": co1 += 1
                if c1 == "I": ci -= 1

                ans = min(ans, dpj[max(0, k-cj)]+dpo[max(0, k-co0-co1)]+dpi[max(0, k-ci)]+cost0+cost1)

if ans >= inf: ans = -1
print(ans)
0