This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ss second
#define ff first
#define vt vector
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int inf = 1e9;
const int mod = 1e9+7;
const int maxn = 1e6 + 1;
void solve() {
int n, k;
string s;
cin >> n >> k >> s;
deque<int> dq[3];
int r[n][3], ans = n + 1;
for(int i = n - 1; i >= 0; i--){
r[i][0] = r[i][1] = r[i][2] = -1;
if(s[i] == 'J'){
dq[0].push_front(i);
if((int)dq[0].size() > k)dq[0].pop_back();
}
else if(s[i] == 'O'){
dq[1].push_front(i);
if((int)dq[1].size() > k)dq[1].pop_back();
}
else {
dq[2].push_front(i);
if((int)dq[2].size() > k)dq[2].pop_back();
}
if((int)dq[0].size() >= k)r[i][0] = dq[0].back();
if((int)dq[1].size() >= k)r[i][1] = dq[1].back();
if((int)dq[2].size() >= k)r[i][2] = dq[2].back();
if(r[i][0] != -1 && r[r[i][0]][1] != -1 && r[r[r[i][0]][1]][2] != -1)ans = min(ans, r[r[r[i][0]][1]][2] - i - k * 3 + 1);
}
cout << (ans == n + 1 ? -1 : ans);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int times = 1;
//cin >> times;
for(int i = 1; i <= times; i++) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |