/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 1ms 320.0 KiB
#3 Accepted 2ms 328.0 KiB
#4 Accepted 2ms 320.0 KiB
#5 Accepted 2ms 324.0 KiB
#6 Accepted 9ms 936.0 KiB
#7 Accepted 19ms 1.883 MiB
#8 Accepted 19ms 1.918 MiB
#9 Accepted 58ms 8.773 MiB
#10 Accepted 171ms 32.992 MiB
#11 Accepted 167ms 32.984 MiB
#12 Accepted 100ms 32.891 MiB
#13 Accepted 169ms 32.98 MiB
#14 Accepted 170ms 32.98 MiB
#15 Accepted 162ms 32.984 MiB
#16 Accepted 152ms 32.984 MiB
#17 Accepted 137ms 32.816 MiB
#18 Accepted 2ms 324.0 KiB

Code

#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif

using namespace std;
// #define int long long
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';

const int INF = 1e8;

void shelby() {
    int n, k;
    string s;
    cin >> n >> k >> s;
    vector<array<int, 3> > need(n);
    auto calc = [&](char x, char y)-> int {
        if (y >= x) return y - x;
        return 26 - (x - y);
    };
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 3; ++j) need[i][j] = calc(s[i], 'a' + j);
    }
    debug(need);
    vector memo(n, vector(n / 3 + 1, -1));
    auto ok = [&](int x)-> bool {
        auto dp = [&](auto &&self, int i, int rem)-> int {
            if (rem == 0) return 0;
            if (i + 2 >= n) return INF;
            auto &ret = memo[i][rem];
            if (~ret) return ret;
            ret = self(self, i + 1, rem);
            ret = min(ret, need[i][0] + need[i + 1][1] + need[i + 2][2] + self(self, i + 3, rem - 1));
            return ret;
        };
        return dp(dp, 0, x) <= k;
    };
    for (int i = n / 3; i >= 0; --i) {
        if (ok(i)) {
            cout << i << '\n';
            return;
        }
    }
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        // cout << "Case " << _ << ": ";
        shelby();
    }
}

Information

Submit By
Type
Submission
Problem
P1100 Substring ABC
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-04 20:42:29
Judged At
2024-11-11 02:41:55
Judged By
Score
100
Total Time
171ms
Peak Memory
32.992 MiB