/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 13ms 336.0 KiB
#3 Accepted 486ms 584.0 KiB
#4 Time Exceeded ≥1598ms ≥572.0 KiB
#5 Time Exceeded ≥1598ms ≥13.27 MiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define F                first
#define S                second
#define pb               push_back
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define P(ok)            cout << (ok ? "YES\n": "NO\n")
#define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll , ll>                   PII;
const int sz = 1e5 + 5 ;
int dp[sz][5] , n ;
string s ;
vector < string > allVowel ;
map < pair < char , char > , int > costAtoB ;
void preCalc(){
    string v = "aeiou" ;
    do {
        allVowel.pb(v) ;
    } while(next_permutation(v.begin() , v.end())) ;
}
void costToTransform() {
 string v = "aeiou" ;
 for(int i = 0 ; i < 5 ; i++) {
    for(int j = 0 ; j < 5 ; j++){
        int cost = (j - i + 5) % 5;
        costAtoB[{v[i] , v[j]}] = cost ;
    }
 }
// for(int i = 0 ; i < 5 ; i++){
//    for(int j = 0 ; j < 5 ; j++) {
//        cout <<v[i] << " " << v[j] << " --> " << costAtoB[{v[i], v[j]}] << endl ;
//    }
// }
}
int heart(int i , int j , const string& curState) {
   if(i == n) return 0 ;
   if(dp[i][j] != -1) return dp[i][j] ;
   int transformCost = costAtoB[{s[i], curState[j]}];
   if(j < 4) {
   dp[i][j] = min(transformCost + heart(i + 1 , j , curState) , transformCost + heart(i + 1 , j + 1 , curState)) ;
   }
   else {
    dp[i][j] = transformCost + heart(i + 1 , j , curState) ;
   }
   return dp[i][j];
}
void solve()
{
  cin >> n >> s ;
  int Ans = INT_MAX ;
  for(auto curState : allVowel){
    for(int i = 0 ; i <= n ; i++) for(int j = 0 ; j < 5 ; j++) dp[i][j] = -1 ;
    Ans = min(Ans , heart(0 , 0 ,  curState)) ;
   // cout << curState << " " << Ans << endl ;
  }
  cout << Ans << "\n" ;
}
int main()
{
     __Heart__
     preCalc() ;
     costToTransform() ;
     int t ; cin >> t ; while(t--) solve() ;
}

Information

Submit By
Type
Submission
Problem
P1140 Vowel arrangement
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-28 23:29:32
Judged At
2024-12-08 18:30:23
Judged By
Score
7
Total Time
≥1598ms
Peak Memory
≥13.27 MiB