/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 584.0 KiB
#2 Accepted 3ms 2.527 MiB
#3 Accepted 3ms 540.0 KiB
#4 Accepted 3ms 540.0 KiB
#5 Accepted 14ms 3.055 MiB
#6 Accepted 56ms 12.801 MiB
#7 Accepted 80ms 24.168 MiB
#8 Accepted 108ms 26.074 MiB
#9 Accepted 497ms 63.797 MiB
#10 Accepted 709ms 129.355 MiB
#11 Accepted 974ms 128.66 MiB
#12 Time Exceeded ≥1101ms ≥142.805 MiB
#13 Accepted 869ms 130.738 MiB
#14 Accepted 861ms 129.453 MiB
#15 Time Exceeded ≥1103ms ≥129.766 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:38:56
Judged At
2024-11-13 19:38:56
Judged By
Score
86
Total Time
≥1103ms
Peak Memory
≥142.805 MiB