/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 5ms 572.0 KiB
#4 Accepted 6ms 564.0 KiB
#5 Accepted 8ms 580.0 KiB
#6 Accepted 5ms 536.0 KiB
#7 Accepted 6ms 484.0 KiB
#8 Accepted 7ms 536.0 KiB
#9 Time Exceeded ≥2049ms ≥19.883 MiB
#10 Accepted 1690ms 19.73 MiB
#11 Accepted 1794ms 19.43 MiB
#12 Accepted 9ms 536.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();

        // For each target letter, keep the best few source letters
        vector<vector<pair<int, int>>> cand(K);  // (cost, pos)

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

        for (int j = 0; j < K; ++j) {
            sort(cand[j].begin(), cand[j].end());
            if (cand[j].size() > 50) cand[j].resize(50);
        }

        // Collect unique source indices
        vector<int> nodes;
        for (int j = 0; j < K; ++j) {
            for (auto [c, pos] : cand[j]) {
                nodes.push_back(pos);
            }
        }
        sort(nodes.begin(), nodes.end());
        nodes.erase(unique(nodes.begin(), nodes.end()), nodes.end());

        int M = nodes.size();

        // Build cost matrix for KM
        vector<vector<int>> cost(K, vector<int>(M, INF));

        for (int j = 0; j < K; ++j) {
            for (auto [c, pos] : cand[j]) {
                int idx = lower_bound(nodes.begin(), nodes.end(), pos) - nodes.begin();
                cost[j][idx] = c;
            }
        }

        // KM Hungarian: find min-cost perfect matching
        // Template for small K

        vector<int> u(K + 1), v(M + 1), p(M + 1), way(M + 1);

        for (int i = 1; i <= K; ++i) {
            p[0] = i;
            vector<int> minv(M + 1, INF);
            vector<bool> used(M + 1, false);
            int j0 = 0;

            do {
                used[j0] = true;
                int i0 = p[j0], delta = INF, j1;
                for (int j = 1; j <= M; ++j) {
                    if (!used[j]) {
                        int cur = cost[i0 - 1][j - 1] - u[i0] - v[j];
                        if (cur < minv[j]) {
                            minv[j] = cur;
                            way[j] = j0;
                        }
                        if (minv[j] < delta) {
                            delta = minv[j];
                            j1 = j;
                        }
                    }
                }
                for (int j = 0; j <= M; ++j) {
                    if (used[j]) {
                        u[p[j]] += delta;
                        v[j] -= delta;
                    } else {
                        minv[j] -= delta;
                    }
                }
                j0 = j1;
            } while (p[j0] != 0);

            do {
                int j1 = way[j0];
                p[j0] = p[j1];
                j0 = j1;
            } while (j0);
        }

        int ans = -v[0];
        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:07:33
Judged At
2025-07-15 16:07:33
Judged By
Score
95
Total Time
≥2049ms
Peak Memory
≥19.883 MiB