/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 116ms 134.113 MiB
#2 Accepted 660ms 134.336 MiB
#3 Time Exceeded ≥1606ms ≥134.316 MiB
#4 Time Exceeded ≥1609ms ≥134.27 MiB

Code

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

#define endl "\n"
#define F first
#define S second
#define pii pair<int, int>
#define sz(x) (int) (x.size())

const int N = 1e5 + 10;
const int mod = 1e9 + 7;
const int INF = 1e18 + 10;

int n;
string s;
int dp[N][35][10];

int mp(char ch) {
    if(ch == 'a') return 0;
    if(ch == 'e') return 1;
    if(ch == 'i') return 2;
    if(ch == 'o') return 3;
    if(ch == 'u') return 4;
    else assert(false);
}

int magic(int ind, int mask, int prv) {
    if(ind == n) return 0;

    int &ans = dp[ind][mask][prv];
    if(~ans) return ans;
    ans = INF;

    for(int i = 0; i < 5; i++) {
        if(mask >> i & 1) continue;
        int op = i - mp(s[ind]);
        if(op < 0) op += 5;
        ans = min(ans, op + magic(ind + 1, mask | (1LL << i), i));
    }

    if(prv != 5) {
        int op = prv - mp(s[ind]);
        if(op < 0) op += 5;
        ans = min(ans, op + magic(ind + 1, mask, prv));
    }

    return ans;
}

void solve() {
    cin >> n >> s;
    memset(dp, -1, sizeof dp);
    cout<<magic(0, 0, 5)<<endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1; 
    cin>>t;
    for(int tc = 1; tc <= t; tc++) {
        // cout<<"Case "<<tc<<":";
        solve();
    }
}

Information

Submit By
Type
Submission
Problem
P1140 Vowel arrangement
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-07 09:43:20
Judged At
2025-07-07 09:43:20
Judged By
Score
4
Total Time
≥1609ms
Peak Memory
≥134.336 MiB