/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 3ms 2.688 MiB
#3 Accepted 2ms 588.0 KiB
#4 Accepted 2ms 588.0 KiB
#5 Accepted 10ms 3.172 MiB
#6 Accepted 38ms 13.07 MiB
#7 Accepted 81ms 25.633 MiB
#8 Accepted 111ms 26.02 MiB
#9 Accepted 513ms 63.863 MiB
#10 Accepted 714ms 130.75 MiB
#11 Accepted 986ms 130.039 MiB
#12 Time Exceeded ≥1103ms ≥142.922 MiB
#13 Accepted 877ms 130.781 MiB
#14 Accepted 871ms 130.766 MiB
#15 Time Exceeded ≥1103ms ≥129.836 MiB

Code

#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define dbg(a,b,c,d) cerr<<a<<"  "<<b<<"  "<<c<<"  "<<d<<endl;
#define kill(a) {cout<<a<<endl;continue;}
#define KILL(a) {cout<<a<<endl;return 0;}
#define debug cerr<<"Error Found"<<endl;
#define mem(a,b) memset(a,b,sizeof(a))
#define lcm(a, b) (a/__gcd(a,b))*b
#define w(t) cin>>t;while(t--)
#define pi  2 * acos(0.0)
#define endl "\n"
int t, cs = 0;
const int mxn = 5e3 + 3, mod = 1e9 + 7;
int vis[mxn][mxn], dp[mxn][mxn];
int n, k;
string s;
int cost(int ch, int tar)
{
    if(ch <= tar)return tar - ch;
    else return 25 - ch + tar + 1;

}
int rec(int i, int cnt)
{
    if(!cnt)return 0;
    if(i >= n - 2)return 1e9;
    if(vis[i][cnt] == cs)return dp[i][cnt];
    vis[i][cnt] = cs;
    int ans = 1e9;
    int a = cost(s[i] - 'a', 0), b = cost(s[i + 1] - 'a', 1), c = cost(s[i + 2] - 'a', 2);
    ans = min(ans, a + b + c + rec(i + 3, cnt - 1));
    ans = min(ans, rec(i + 1, cnt));
    return dp[i][cnt] = ans;
}
int32_t main()
{
    fast;
    w(t)
    {
        cin >> n >> k;
        cin >> s;
        int low = 1, high = n / 3;
        while(low <= high)
        {
            int mid = low + high >> 1;
            ++cs;
            int cost = rec(0, mid);

            if(cost <= k)low = mid + 1;
            else high = mid - 1;
        }
        cout << --low << endl;
    }

}

Information

Submit By
Type
Submission
Problem
P1100 Substring ABC
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-13 19:39:29
Judged At
2024-11-13 19:39:29
Judged By
Score
86
Total Time
≥1103ms
Peak Memory
≥142.922 MiB