/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 468ms 564.0 KiB
#2 Accepted 646ms 556.0 KiB
#3 Time Exceeded ≥2098ms ≥560.0 KiB
#4 Time Exceeded ≥2099ms ≥556.0 KiB

Code

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

const int INF = 1e9;

int shiftCost(char a, char b) {
    int d = abs(a - b);
    return min(d, 26 - d);
}

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

    int T;
    cin >> T;

    string target = "SERIOUSOJ";
    int K = 9;

    while (T--) {
        string S;
        cin >> S;
        int n = S.size();

        vector<vector<pair<int, int>>> costs(K); // (cost, pos)

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < K; ++j) {
                int c = shiftCost(S[i], target[j]);
                costs[j].emplace_back(c, i);
            }
        }

        // For each target letter, keep only best 20 source letters
        for (int j = 0; j < K; ++j) {
            sort(costs[j].begin(), costs[j].end());
            if (costs[j].size() > 20) costs[j].resize(20);
        }

        int ans = INF;

        vector<int> perm(K);
        iota(perm.begin(), perm.end(), 0);

        do {
            // Try this target letter order
            int total = 0;
            set<int> used_pos;

            bool valid = true;

            for (int j = 0; j < K; ++j) {
                bool found = false;
                for (auto [c, pos] : costs[perm[j]]) {
                    if (used_pos.count(pos)) continue;
                    total += c;
                    used_pos.insert(pos);
                    found = true;
                    break;
                }
                if (!found) {
                    valid = false;
                    break;
                }
            }

            if (valid) ans = min(ans, total);

        } while (next_permutation(perm.begin(), perm.end()));

        cout << ans << '\n';
    }
}

Information

Submit By
Type
Submission
Problem
P1188 H. The Mysty Lock
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-15 16:04:10
Judged At
2025-07-15 16:04:10
Judged By
Score
10
Total Time
≥2099ms
Peak Memory
≥564.0 KiB