/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 1ms 324.0 KiB
#3 Accepted 2ms 324.0 KiB
#4 Accepted 2ms 532.0 KiB
#5 Accepted 3ms 576.0 KiB
#6 Accepted 17ms 1.551 MiB
#7 Accepted 37ms 4.477 MiB
#8 Accepted 36ms 4.473 MiB
#9 Accepted 107ms 24.699 MiB
#10 Accepted 274ms 96.633 MiB
#11 Accepted 267ms 96.641 MiB
#12 Accepted 181ms 96.637 MiB
#13 Accepted 267ms 96.637 MiB
#14 Accepted 271ms 96.629 MiB
#15 Accepted 259ms 96.637 MiB
#16 Accepted 241ms 96.641 MiB
#17 Accepted 221ms 96.641 MiB
#18 Accepted 2ms 532.0 KiB

Code

#include<bits/stdc++.h>
#define endl        '\n'
#define F           first
#define S           second
#define pb          push_back
#define yes         cout<<"YES\n"
#define no          cout<<"NO\n"
#define all(x)      x.begin(),x.end()
#define allr(x)     x.rbegin(),x.rend()
#define error1(x)   cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v)  for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;
using ld = long double;

const int inf = 1e9;

void solve() {
    ll n, k;
    string s1;
    cin >> n >> k >> s1;
    
    auto cost = [&](char now, char target) -> int  {
        if(now <= target) return target - now;
        return ('z' - now) + (target - 'a') + 1;
    };

    vector<vector<int>> dp(n + 1, vector<int> (n + 1, -1));
    auto rec = [&] (auto&& self, int i, int make) -> int {
        if(i + 2 >= n) return (make == 0 ? 0 : inf);
        auto& ans = dp[i][make];
        if(ans != -1) return ans;
        ans = self(self, i + 1, make);
        if(make > 0) ans = min(ans, cost(s1[i], 'a') + cost(s1[i + 1], 'b') + cost(s1[i + 2], 'c') + self(self,i + 3, make - 1));
        return ans >= inf ? inf : ans;
    };

    for(int i = n / 3; i >= 1; i--) {
        if(rec(rec, 0, i) <= k) {
            cout << i << endl;
            return;
        }
    }
    cout << 0 << endl;
    return;
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1100 Substring ABC
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-09 09:19:31
Judged At
2024-11-11 02:38:13
Judged By
Score
100
Total Time
274ms
Peak Memory
96.641 MiB