/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 47ms 18.09 MiB
#2 Accepted 54ms 18.871 MiB
#3 Accepted 105ms 19.301 MiB
#4 Accepted 106ms 19.543 MiB
#5 Accepted 77ms 19.613 MiB
#6 Accepted 91ms 23.859 MiB
#7 Accepted 126ms 44.277 MiB
#8 Accepted 127ms 43.824 MiB
#9 Accepted 192ms 59.969 MiB
#10 Accepted 334ms 125.438 MiB
#11 Accepted 316ms 125.43 MiB
#12 Accepted 335ms 125.426 MiB
#13 Accepted 327ms 125.438 MiB
#14 Accepted 335ms 125.418 MiB
#15 Accepted 325ms 125.414 MiB
#16 Accepted 327ms 125.402 MiB
#17 Accepted 322ms 125.438 MiB
#18 Accepted 93ms 19.254 MiB

Code

#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict
from itertools import *
import random
import math

# MOD = 998244353
MOD = 10**9 + 7

inf = float("inf")


def solve():
    N, K = rlist()
    orda = ord("a")
    A = [ord(c) - orda for c in input()]

    cost = [0] * (N - 2)
    for i in range(N - 2):
        cur_cost = 0
        for j in range(3):
            x = A[i + j]
            if x == j:
                continue
            elif x > j:
                cur_cost += 26 + j - x
            else:
                cur_cost += j - x
        cost[i] = cur_cost

    dp = [[inf] * (N // 3 + 1) for _ in range(N + 1)]
    for i in range(N + 1):
        dp[i][0] = 0

    for i in range(N - 3, -1, -1):
        for s in range(N // 3, 0, -1):
            dp[i][s] = min(dp[i + 1][s], dp[i + 3][s - 1] + cost[i])

    for s in range(N // 3, -1, -1):
        if dp[0][s] <= K:
            return s


def main():
    T = 1
    T = rint()

    for tc in range(1, 1 + T):
        ans = solve()

        print(ans)
        # print(*ans)

        # print("Yes" if ans else "No")
        # print("YES" if ans else "NO")
        # print("Alice" if ans else "Bob")
        # print("First" if ans else "Second")
        # print("Case #{}: {}".format(tc, ans))
        # print(len(ans))
        # for row in ans: print(*row)


rint = lambda: int(input())


def rlist():
    return list(map(int, input().split()))


def rgrid(n):
    return [rlist() for _ in range(n)]


# endregion

if __name__ == "__main__":
    main()

Information

Submit By
Type
Submission
Problem
P1100 Substring ABC
Language
PyPy 3 (Python 3.9.18 PyPy 7.3.15)
Submit At
2024-10-20 20:09:37
Judged At
2024-11-11 02:36:31
Judged By
Score
100
Total Time
335ms
Peak Memory
125.438 MiB