/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 13ms 540.0 KiB
#3 Accepted 429ms 584.0 KiB
#4 Accepted 1001ms 816.0 KiB
#5 Time Exceeded ≥1597ms ≥91.5 MiB
#6 Time Exceeded ≥1600ms ≥91.656 MiB

Code

/**
*  Problem Name: Vowel_arrangement
*  Author: MJS
*  Created: 12 December 2024, Thursday (13:02:07)...
**/

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

#define ll long long int
#define nl '\n' 
#define inf -1

string v="aeiou";
int cost[5][5];
map<char,int> id;

int n;
string st;
int dp[100003][33][6];

int f(int i,int j,char lst){
    if(i==n)    return 0;
    
    int k=((lst=='*')?0:id[lst]+1);
    if(dp[i][j][k]!=inf)    return dp[i][j][k];

    int take=1e8,not_take=1e8;
    int m1=j;
    if(((1<<(id[st[i]]))&m1)==0){
        if(lst!='*' && st[i]!=lst){
            m1|=(1<<(id[lst]));
        }
        not_take=0+f(i+1,m1,st[i]);
    }
    for(auto &c: v){
        int m2=j;
        if(((1<<(id[c]))&m2)==0){
            if(lst!='*' && c!=lst){
                m2|=(1<<(id[lst]));
            }  
            take=min(take,cost[id[st[i]]][id[c]]+f(i+1,m2,c));
        }
    }
    return dp[i][j][k]=min(take,not_take);
}

void answer_to_the_question(){
    cin>>n>>st;
    for(int i=0;i<n+3;i++){
        for(int j=0;j<33;j++){
            for(int k=0;k<6;k++){
                dp[i][j][k]=inf;
            }
        }
    }
    cout<<f(0,0,'*')<<nl;
}

int32_t main(){
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int test_case;    cin>>test_case;

    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            cost[i][j]=(10+j-i)%5;
        }
    }
    for(int i=0;i<5;i++)    id[v[i]]=i;

    for(int MJS=1; MJS<=test_case; MJS++){
        answer_to_the_question();
    }

    return 0;
}

// Hi..

Information

Submit By
Type
Submission
Problem
P1140 Vowel arrangement
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-12 12:16:58
Judged At
2024-12-12 12:16:58
Judged By
Score
10
Total Time
≥1600ms
Peak Memory
≥91.656 MiB