/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 536.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 2ms 344.0 KiB
#4 Accepted 2ms 564.0 KiB
#5 Accepted 6ms 580.0 KiB
#6 Accepted 357ms 1.016 MiB
#7 Time Exceeded ≥1091ms ≥2.117 MiB
#8 Time Exceeded ≥1084ms ≥2.09 MiB

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;
    };
    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:40:45
Judged At
2024-11-11 02:41:59
Judged By
Score
37
Total Time
≥1091ms
Peak Memory
≥2.117 MiB