/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 492.0 KiB
#2 Accepted 12ms 600.0 KiB
#3 Accepted 546ms 828.0 KiB
#4 Accepted 1472ms 572.0 KiB
#5 Time Exceeded ≥1600ms ≥13.836 MiB
#6 Time Exceeded ≥1596ms ≥13.793 MiB

Code

#include <bits/stdc++.h>
using namespace std;
const long long M = 2e5 + 10, MOD = 1000000007;
typedef long long ll;
int cost[26][26];
vector<string> all;

void precal() {
    string vowel = "aeiou";
    for (int i = 0; i < (int)vowel.size(); i++) {
        int l = i;
        int cnt = 0;
        while (cnt < 5) {
            cost[vowel[i] - 'a'][vowel[l] - 'a'] = cnt;
            l++;
            cnt++;
            if (l == 5) l = 0;
        }
    }
    all.push_back(vowel);
    while (next_permutation(vowel.begin(), vowel.end())) {
        all.push_back(vowel);
    }
}

int solve(int idx, int p_idx, string &s, string &p, vector<vector<int>> &memo) {
    if (idx == 0) return 0; 

    if (memo[idx][p_idx] != -1) return memo[idx][p_idx]; // Use memoized value

    int pro = cost[s[idx - 1] - 'a'][p[p_idx] - 'a'];

    int option1 = solve(idx - 1, p_idx, s, p, memo) + pro;

    int option2 = INT_MAX;
    if (p_idx > 0) {
        option2 = solve(idx, p_idx - 1, s, p, memo);
    }

    return memo[idx][p_idx] = min(option1, option2);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    precal();

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        string s;
        cin >> s;

        int mx = INT_MAX;
        for (auto &it : all) {
            string p = it;
            vector<vector<int>> memo(n + 1, vector<int>(5, -1));
            for (int i = 0; i < 5; i++) {
                mx = min(mx, solve(n, i, s, p, memo));
            }
        }

        cout << mx << "\n";
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1140 Vowel arrangement
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-16 06:12:14
Judged At
2024-12-16 06:12:14
Judged By
Score
10
Total Time
≥1600ms
Peak Memory
≥13.836 MiB