/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 540.0 KiB
#2 Accepted 86ms 640.0 KiB
#3 Time Exceeded ≥1597ms ≥620.0 KiB
#4 Time Exceeded ≥1599ms ≥980.0 KiB

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<set<char>,int> type;
map<char,int> id;

int n;
string st;
vector<vector<vector<int>>> dp;
// int dp[N][33][6];,

void taking(set<char> s){
    if(type.find(s)!=type.end())    return;

    type[s]=type.size();

    for(auto &c: v){
        auto t=s;
        if(t.find(c)==t.end()){
            t.insert(c);
            taking(t);
        }
        taking(s);
    }
}

int f(int i,set<char> s,char lst){
    if(i==n)    return 0;
    
    int j=type[s],k=id[lst];
    if(dp[i][j][k]!=-1)    return dp[i][j][k];

    int take=1e8,not_take=1e8;
    auto tmp=s;
    if(tmp.find(st[i])==tmp.end()){
        if(lst!='*' && st[i]!=lst)  tmp.insert(lst);
        not_take=0+f(i+1,tmp,st[i]);
    }
    for(auto &c: v){
        auto t=s;
        if(t.find(c)==t.end()){
            if(lst!='*' && c!=lst)  t.insert(lst);
            take=min(take,cost[{st[i],c}]+f(i+1,t,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)));

    set<char> tmp;
    cout<<f(0,tmp,'*')<<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[{v[i],v[j]}]=(10+j-i)%5;
        }
    }
    set<char> s;
    taking(s);
    for(int i=0;i<5;i++)    id[v[i]]=i+1;
    id['*']=0;

    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 08:32:25
Judged At
2024-12-12 08:32:25
Judged By
Score
4
Total Time
≥1599ms
Peak Memory
≥980.0 KiB