/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 580.0 KiB
#3 Accepted 2ms 532.0 KiB
#4 Accepted 2ms 324.0 KiB
#5 Accepted 3ms 616.0 KiB
#6 Accepted 14ms 1.52 MiB
#7 Accepted 29ms 4.441 MiB
#8 Accepted 30ms 4.473 MiB
#9 Accepted 81ms 24.695 MiB
#10 Accepted 159ms 96.637 MiB
#11 Accepted 160ms 96.641 MiB
#12 Accepted 207ms 96.637 MiB
#13 Accepted 159ms 96.484 MiB
#14 Accepted 162ms 96.641 MiB
#15 Accepted 161ms 96.641 MiB
#16 Accepted 184ms 96.637 MiB
#17 Accepted 193ms 96.637 MiB
#18 Accepted 2ms 344.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;
    };

    int lo = 1, hi = n / 3, mid, ans = 0;
    while(lo <= hi) {
        mid = lo + hi >> 1;

        if(rec(rec, 0, mid) <= k) {
            ans = mid;
            lo = mid + 1;
        }
        else hi = mid - 1;
    }
    cout << ans << 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:24:23
Judged At
2024-11-11 02:38:12
Judged By
Score
100
Total Time
207ms
Peak Memory
96.641 MiB