/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 18ms 796.0 KiB
#3 Accepted 577ms 632.0 KiB
#4 Accepted 1216ms 1.027 MiB
#5 Time Exceeded ≥1612ms ≥195.371 MiB
#6 Time Exceeded ≥1607ms ≥195.43 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 N 100003

string v="aeiou";
map<pair<char,char>,int> cost;
map<char,int> id;

int n;
string st;
vector<vector<vector<int>>> dp;

int f(int i,int j,char lst){
    if(i==n)    return 0;
    
    int k=id[lst];
    if(lst=='*')    k=0;
    else            k++;
    if(dp[i][j][k]!=-1)    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[{st[i],c}]+f(i+1,m2,c));
        }
    }
    return dp[i][j][k]=min(take,not_take);
}

void answer_to_the_question(){
    cin>>n>>st;
    dp=vector(n+2,vector(33,vector(6,-1)));

    cout<<f(0,0,'*')<<nl;
}

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

    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            cost[{v[i],v[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:04:57
Judged At
2024-12-12 12:04:57
Judged By
Score
10
Total Time
≥1612ms
Peak Memory
≥195.43 MiB