/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 332.0 KiB
#2 Accepted 19ms 580.0 KiB
#3 Accepted 631ms 596.0 KiB
#4 Time Exceeded ≥1595ms ≥636.0 KiB
#5 Time Exceeded ≥1537ms ≥75.543 MiB

Code

#include<bits/stdc++.h>
using namespace std;

#define nl '\n'
#define ll long long
// #define int long long
#define pii pair<int, int> 

const int N = 1e5+5;
const int inf = 2e9;
int n;
string s, t = "aeiouaeiou";

map<pair<char,char>, int> mvv;

int chk(char x, char c){
    int ret = 0, id = 0;
    while(t[id]!= x) id++;
    while(t[id]!= c) id++, ret++;
    return ret;
}

int dp[N][5][(1<<5)];

int go(int pos, int c, int mask){
    if(pos == n) return 0;
    if(__builtin_popcount(mask) == 5) return inf;
    int &ret = dp[pos][c][mask];
    if(~ret) return ret;
    int ans = inf;
    ans = min(ans, mvv[{s[pos], t[c]}] + go(pos+1, c, mask));
    for(int i=0;i<5;++i){
        if(mask&(1<<i)) continue;
        ans = min(ans, mvv[{s[pos], t[i]}] + go(pos+1, i, mask|(1<<c)));
    }
    return ret = ans;
}

void solve(){
    cin >> n >> s;
    int ans = inf;
    for(int i=0;i<=n;++i){
        memset(dp[i], -1, sizeof dp[i]);
    }
    for(int i=0;i<5;++i){
        ans = min(ans, (go(0,i,0)==-1?inf:go(0,i,0)));
        //
    }
    cout << ans << nl;
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    for(int i=0;i<5;++i){
        for(int j=0;j<5;++j){
            if(i==j) mvv[{t[i], t[j]}] = 0;
            else mvv[{t[i], t[j]}] = chk(t[i], t[j]);
        }
    }

    int t = 1, tc = 0;
    cin >> t ;
    while(t--){
        // cout << "Case " << ++tc  << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1140 Vowel arrangement
Contest
LU IUJPC : Sylhet Division 2024
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-09 09:23:08
Judged At
2024-12-09 09:23:08
Judged By
Score
7
Total Time
≥1595ms
Peak Memory
≥75.543 MiB