/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 576.0 KiB
#2 Accepted 5ms 532.0 KiB
#3 Accepted 36ms 620.0 KiB
#4 Accepted 26ms 616.0 KiB
#5 Accepted 94ms 748.0 KiB
#6 Accepted 21ms 604.0 KiB
#7 Accepted 35ms 576.0 KiB
#8 Accepted 69ms 532.0 KiB
#9 Memory Exceeded ≥2104ms ≥256.016 MiB
#10 Time Exceeded ≥2006ms ≥99.09 MiB

Code

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;

void bruteForce() {
    string s1;
    cin >> s1;
    int n = s1.size();
    string target = "SERIOUSOJ";

    auto Cost2 = [&](char ch1, char ch2) -> int {
        if(ch1 > ch2) swap(ch1, ch2); 
        // ch1 <= ch2
        int mn = abs(ch2 - ch1), cnt = 0;
        for(char ch = ch1; ch != ch2; ) {
            --ch; ++cnt;
            if(ch < 'A') ch = 'Z';
        }
        return min(mn, cnt);
    };

    vector<vector<int>> dp(512, vector<int> (s1.size(), -1));
    auto rec = [&](auto&& self, int i, bitset<9> mask) -> int {
        if(mask.count() == 9) return 0;
        if(i >= s1.size()) return 1e9;
        auto& ans = dp[mask.to_ullong()][i];
        if(ans != -1) return ans;
        ans = self(self, i + 1, mask); // not take
        for(int j = 0; j < 9; j++) {
            if(mask[j]) continue;
            mask[j] = 1;
            ans = min(ans, Cost2(s1[i], target[j]) + self(self, i + 1, mask));
            mask[j] = 0;
        }
        return ans;
    };
    cout << rec(rec, 0, 0) << endl;
    return;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        bruteForce();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1188 The Mysty Lock
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-04 17:04:07
Judged At
2025-04-04 17:04:07
Judged By
Score
40
Total Time
≥2104ms
Peak Memory
≥256.016 MiB