/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 328.0 KiB
#2 Accepted 1ms 320.0 KiB
#3 Accepted 2ms 320.0 KiB
#4 Accepted 2ms 320.0 KiB
#5 Accepted 6ms 576.0 KiB
#6 Accepted 19ms 960.0 KiB
#7 Accepted 43ms 1.812 MiB
#8 Accepted 63ms 1.93 MiB
#9 Accepted 272ms 6.785 MiB
#10 Accepted 241ms 17.008 MiB
#11 Accepted 378ms 17.008 MiB
#12 Time Exceeded ≥1056ms ≥32.988 MiB
#13 Accepted 321ms 16.953 MiB
#14 Accepted 291ms 16.902 MiB
#15 Accepted 566ms 17.0 MiB
#16 Accepted 848ms 25.039 MiB
#17 Accepted 971ms 28.777 MiB
#18 Accepted 2ms 564.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);
    auto ok = [&](int x)-> bool {
        vector memo(n, vector(x + 1, -1));
        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;
    };
    int l = 0, r = n / 3, ans = 0;
    while (l <= r) {
        int m = (l + r) / 2;
        if (ok(m)) ans = m, l = m + 1;
        else r = m - 1;
    }
    cout << ans << '\n';
}

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:37:47
Judged At
2024-11-11 02:42:04
Judged By
Score
93
Total Time
≥1056ms
Peak Memory
≥32.988 MiB