/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 532.0 KiB
#2 Accepted 2ms 324.0 KiB
#3 Accepted 6ms 532.0 KiB
#4 Accepted 6ms 764.0 KiB
#5 Accepted 22ms 532.0 KiB
#6 Accepted 6ms 532.0 KiB
#7 Accepted 10ms 324.0 KiB
#8 Accepted 16ms 532.0 KiB
#9 Time Exceeded ≥2100ms ≥788.0 KiB
#10 Time Exceeded ≥2099ms ≥664.0 KiB

Code

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

const int INF = 1e9;

int minShiftCost(char from, char to) {
    int d = abs(from - to);
    return min(d, 26 - d);
}

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

    int T;
    cin >> T;

    string target = "SERIOUSOJ";

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

        vector<int> dp(1 << 9, INF);
        dp[0] = 0;

        for (int i = 0; i < n; ++i) {
            vector<int> ndp = dp;  // copy previous dp

            for (int mask = 0; mask < (1 << 9); ++mask) {
                if(i + 1 < __builtin_popcount(mask))
                  continue;
                
                for (int j = 0; j < 9; ++j) {
                    if ((mask & (1 << j)) == 0) {
                        int newMask = mask | (1 << j);
                        int cost = minShiftCost(S[i], target[j]);
                        ndp[newMask] = min(ndp[newMask], dp[mask] + cost);
                    }
                }
            }

            dp = move(ndp);
        }

        cout << dp[(1 << 9) - 1] << '\n';
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1188 H. The Mysty Lock
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-15 16:01:24
Judged At
2025-07-15 16:01:24
Judged By
Score
40
Total Time
≥2100ms
Peak Memory
≥788.0 KiB