/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Wrong Answer 25ms 552.0 KiB
#3 Accepted 1335ms 572.0 KiB
#4 Time Exceeded ≥1530ms ≥540.0 KiB

Code

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

void solve() {
    int n; cin >> n;
    string s; cin >> s;
    string x = "aeiou";
    string v = x;
    auto dist = [&](char a, char b) -> int {
        int x = v.find(a), y = v.find(b);
        int res = (y >= x ? y - x : 5 + y - x);
        // cout << a << " " << b << '\n';
        // cout << x << " " << y << '\n';
        // cout << "res " << res << '\n';
        return res;
    };
    int ans = n + 1;
    do {
        // cout << x << '\n';
        vector<int> dp(5);
        for (int i = 0; i < n; i++) {
            for (int j = 4; j > 0; j--) {
                dp[j] = min(dp[j] + dist(s[i], x[j]), (j - 1 >= 0 ? dp[j - 1] : 0) + dist(s[i], x[j]));
            }
            dp[0] += dist(s[i], x[0]);
            // for (auto& a : dp) cout << a << " "; cout << '\n';
        }
        ans = min(ans, *min_element(dp.begin(), dp.end()));
        // cout << "ans " << ans << '\n';
    } while (next_permutation(x.begin(), x.end()));
    cout << ans << '\n';
} 

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt = 1;
    cin >> tt;
    for(int t = 1; t <= tt; t++) {
        solve();
    }
}

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 12:20:08
Judged At
2024-12-10 12:20:08
Judged By
Score
4
Total Time
≥1530ms
Peak Memory
≥572.0 KiB