/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 2ms 540.0 KiB
#2 Accepted 3ms 768.0 KiB
#3 Accepted 11ms 816.0 KiB
#4 Accepted 12ms 812.0 KiB
#5 Accepted 26ms 772.0 KiB
#6 Accepted 10ms 540.0 KiB
#7 Accepted 15ms 772.0 KiB
#8 Accepted 27ms 652.0 KiB
#9 Accepted 575ms 1.082 MiB
#10 Accepted 463ms 1.039 MiB
#11 Accepted 471ms 1.117 MiB
#12 Accepted 53ms 712.0 KiB

Code

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

int Cost(char ch1, char ch2) {
    return min({abs(ch1 - ch2), ('Z' - ch1) + 1 + (ch2 - 'A'), (ch1 - 'A') + 1 + ('Z' - ch2)});
}

void solve() {
    string s1, s2 = "", target = "SERIOUSOJ";
    cin >> s1;
    int n = s1.size(), m = target.size();
    vector<bool> vis(n);
    for(int j = 0; j < m; j++) {
        priority_queue<pair<int, int>> pq;
        for(int i = 0; i < n; i++) {
            pq.push({Cost(s1[i], target[j]), i});
            if(pq.size() > 9) pq.pop();
        }
        while(pq.size()) {
            if(vis[pq.top().second] == 0) {
                vis[pq.top().second] = 1;
                s2 += s1[pq.top().second];
            }
            pq.pop();
        }
    }

    vector<vector<int>> dp(512, vector<int> (s2.size(), -1));
    auto rec = [&](auto&& self, int i, bitset<9> &mask) -> int {
        if(mask.count() == 9) return 0;
        if(i >= s2.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, Cost(s2[i], target[j]) + self(self, i + 1, mask));
            mask[j] = 0;
        }
        return ans;
    };
    bitset<9> mask = 0;
    cout << rec(rec, 0, mask) << 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 << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1188 The Mysty Lock
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-04 16:53:18
Judged At
2025-04-04 16:53:18
Judged By
Score
95
Total Time
575ms
Peak Memory
1.117 MiB