Submission #956473

#TimeUsernameProblemLanguageResultExecution timeMemory
956473steveonalexJJOOII 2 (JOI20_ho_t2)C++17
100 / 100
18 ms43240 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1ULL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ull mask){return __builtin_ctzll(mask);} int logOf(ull mask){return 63 - __builtin_clzll(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){ for(auto item: container) out << item << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } const int N = 2e5 + 69, LOG_N = 18; const int INF = 1e9 + 69; int nxt[3][LOG_N][N]; int lift(int j, int i, int d){ for(int t = 0; t < LOG_N; ++t) if (GETBIT(d, t)) i = nxt[j][t][i]; return i; } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; string s; cin >> s; for(int j = 0; j<3; ++j) nxt[j][0][n] = n; for(int i = n-1; i>=0; --i){ for(int j = 0; j < 3; ++j) nxt[j][0][i] = nxt[j][0][i+1]; if (i < n - 1){ if (s[i + 1] == 'J') nxt[0][0][i] = i + 1; else if (s[i+1] == 'O') nxt[1][0][i] = i + 1; else nxt[2][0][i] = i + 1; } } for(int j = 0; j < 3; ++j){ for(int t = 1; t < LOG_N; ++ t){ for(int i = 0; i<=n; ++i) nxt[j][t][i] = nxt[j][t-1][nxt[j][t-1][i]]; } } int ans = INF; for(int i = 0; i<n; ++i) if (s[i] == 'J'){ int j = i; j = lift(0, j, k-1); j = lift(1, j, k); j = lift(2, j, k); if (j == n) break; minimize(ans, j - i + 1 - 3 * k); } if (ans == INF) cout << -1 << "\n"; else cout << ans << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...