/ SeriousOJ /

Record Detail

Runtime Error


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 532.0 KiB
#2 Accepted 2ms 532.0 KiB
#3 Accepted 121ms 10.551 MiB
#4 Accepted 138ms 10.418 MiB
#5 Runtime Error 3ms 2.609 MiB
#6 Accepted 16ms 788.0 KiB
#7 Memory Exceeded ≥2231ms ≥256.016 MiB

Code

#include <bits/stdc++.h>
#define ll long long int
#define N "\n"
using namespace std;
ll shift(char a, char b)
{
      ll d = abs(a - b);
      return min(d, 26 - d);
}
int main()
{
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      ll t;
      cin >> t;
      while (t--)
      {
            string s;
            cin >> s;
            string tar = "SERIOUSOJ";
            unordered_map<char, int> freq;
            vector<char> store;
            for (auto c : s)
            {
                  freq[c]++;
                  if (freq[c] <= tar.size())
                        store.push_back(c);
            }
            vector<vector<ll>> cost(tar.size(), vector<ll>((store.size())));
            for (ll i = 0; i < tar.size(); i++)
            {
                  for (ll j = 0; j < store.size(); j++)
                  {
                        cost[i][j] = shift(store[j], tar[i]);
                  }
            }

            vector<ll> dp(1ll << (store.size()), INT_MAX);
            dp[0] = 0;
            ll ans = INT_MAX;
            for (ll i = 0; i < 1ll << store.size(); i++)
            {
                  ll assign = __builtin_popcount(i);
                  if (assign < tar.size())
                  {
                        for (ll j = 0; j < store.size(); j++)
                        {
                              if (!(i & (1ll << j)))
                              {
                                    ll new_i = (1ll << j) | i;
                                    dp[new_i] = min(dp[new_i], dp[i] + cost[assign][j]);
                              }
                        }
                  }
            }
            for (ll i = 0; i < 1ll << store.size(); i++)
            {
                  if (__builtin_popcount(i) == tar.size())
                  {
                        ans = min(ans, dp[i]);
                  }
            }
            cout << ans << N;
      }
}

Information

Submit By
Type
Submission
Problem
P1188 The Mysty Lock
Contest
Brain Booster #9
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-06 17:52:35
Judged At
2025-04-06 17:52:35
Judged By
Score
25
Total Time
2231ms
Peak Memory
256.016 MiB