/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 45ms 18.121 MiB
#2 Accepted 53ms 18.891 MiB
#3 Accepted 104ms 19.137 MiB
#4 Accepted 103ms 19.551 MiB
#5 Accepted 75ms 19.539 MiB
#6 Accepted 90ms 23.855 MiB
#7 Accepted 128ms 44.348 MiB
#8 Accepted 122ms 43.941 MiB
#9 Accepted 183ms 59.973 MiB
#10 Accepted 329ms 125.43 MiB
#11 Accepted 313ms 125.445 MiB
#12 Accepted 329ms 125.426 MiB
#13 Accepted 331ms 125.355 MiB
#14 Accepted 329ms 125.441 MiB
#15 Accepted 323ms 125.219 MiB
#16 Accepted 326ms 125.441 MiB
#17 Accepted 332ms 125.449 MiB
#18 Accepted 98ms 19.363 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
2025-02-09 14:04:10
Judged By
Score
100
Total Time
332ms
Peak Memory
125.449 MiB