/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 190ms 192.164 MiB
#2 Accepted 192ms 192.105 MiB
#3 Accepted 190ms 192.207 MiB
#4 Accepted 181ms 192.16 MiB
#5 Accepted 214ms 192.211 MiB
#6 Accepted 280ms 192.223 MiB
#7 Accepted 398ms 192.254 MiB
#8 Accepted 423ms 192.25 MiB
#9 Time Exceeded ≥1021ms ≥192.199 MiB
#10 Time Exceeded ≥1038ms ≥192.211 MiB

Code

// PIPRA ||  HABIB
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define int        long long int
#define pb         push_back
#define all(x)     x.begin(),x.end()
#define allr(x)    x.rbegin(),x.rend()
#define ii         pair<int,int>
#define endl       "\n"

template<typename T>
ostream& operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for (const auto &x : v) os << " " << x;
        return os << '}';
}
using orderedTree = tree<int, null_type, less<int>, 
rb_tree_tag, tree_order_statistics_node_update>;

const int N = 5005;
const int inf = 1e8;

int n;
string s;
vector<vector<int>> dp(N, vector<int>(N));

int recur(int i, int cnt){
    if(cnt == 0)    return 0;
    if(i >= (n-2))  return inf;

    int &ans = dp[i][cnt];
    if(ans != -1)   return ans;

    ans = recur(i+1, cnt);

    
    int ind = i;
    int cost = 0;
    for(char ch='a' ; ch<='c' ; ch++){
        if(s[ind] <= ch){
            cost += ch - s[ind];
        }
        else{
            cost += 'z' - s[ind] + (ch - 'a' + 1);
        }
        ind++;
    }

    ans = min(ans, cost + recur(i+3, cnt-1));
    return ans;
}

void pipra(){
    int k;
    cin >> n >> k;
    cin >> s;

    for(int i=0 ; i<=n ; i++)
        for(int j=0 ; j<=n ; j++)
            dp[i][j] = -1;

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

    cout << 0 << endl;
}

int32_t main(){
    // HABIB
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); cout.tie(NULL);

    int t;    cin>>t;
    while(t--) {
        pipra();
    }
    return 0 ;
}

Information

Submit By
Type
Submission
Problem
P1100 Substring ABC
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-12 06:32:34
Judged At
2024-11-11 02:37:45
Judged By
Score
51
Total Time
≥1038ms
Peak Memory
≥192.254 MiB