/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 580.0 KiB
#2 Accepted 9ms 540.0 KiB
#3 Accepted 267ms 576.0 KiB
#4 Accepted 1235ms 584.0 KiB
#5 Accepted 1307ms 12.215 MiB
#6 Accepted 1310ms 12.211 MiB
#7 Accepted 1291ms 7.793 MiB
#8 Accepted 1287ms 3.535 MiB
#9 Accepted 602ms 584.0 KiB
#10 Accepted 565ms 576.0 KiB
#11 Accepted 1308ms 12.285 MiB
#12 Accepted 1312ms 12.25 MiB
#13 Accepted 1054ms 12.215 MiB

Code

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll n,dp[100002][5];
string s;
vector<ll>v;
ll f(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;
  }
}
ll rec(ll i,ll ty){
  if(i>=n){
    return 0;
  }
  ll &ans=dp[i][ty];
  if(~ans){
    return ans;
  }
  ll mn=1e9,vi=v[ty],ci=f(s[i]);
  if(ci<=vi){
    mn=min(mn,vi-ci+rec(i+1,ty));
  }
  else {
    mn=min(mn,vi+5-ci+rec(i+1,ty));
  }
  if(ty==4){
    return ans=mn;
  }
  vi=v[ty+1];
  if(ci<=vi){
    mn=min(mn,vi-ci+rec(i+1,ty+1));
  }
  else {
    mn=min(mn,vi+5-ci+rec(i+1,ty+1));
  }
  return ans=mn;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tc=1,tc1=1;
    cin>>tc;
    up:
    while(tc--){
      cin>>n>>s;
      v.clear();
      for(ll i=0;i<5;i++){
        v.push_back(i);
      }
      ll mn=1e18;
      do{
       for(ll i=0;i<=n;i++){
        for(ll j=0;j<=4;j++){
          dp[i][j]=-1;
        }
       }
       ll here=rec(0,0);
       mn=min(mn,here);
      }while(next_permutation(v.begin(),v.end()));
      cout<<mn<<'\n';
    }
}

Information

Submit By
Type
Submission
Problem
P1140 Vowel arrangement
Contest
LU IUJPC : Sylhet Division 2024 Replay Contest
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-10 13:29:35
Judged At
2024-12-10 13:29:35
Judged By
Score
100
Total Time
1312ms
Peak Memory
12.285 MiB