이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=5005, INF=1e18;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k; cin >> n >> k;
vector<vector<int>> pref(n, vector<int>(n, 0));
string s; cin >> s;
for(int i =0; i<n; i++){
if(s[i]=='J') pref[0][i]++;
else if(s[i]=='O') pref[1][i]++;
else pref[2][i]++;
if(i>0){
pref[0][i]+=pref[0][i-1];
pref[1][i]+=pref[1][i-1];
pref[2][i]+=pref[2][i-1];
}
}
auto val=[&](int l, int r, int type){
if(l>r) return 0LL;
if(r==n) return 0LL;
if(l==0) return pref[type][r];
return pref[type][r]-pref[type][l-1];
};
int posj=0, poso=0, posi=0, ans=n+1;
for(int i=0; i<n; i++){
while(posj<n&&val(i, posj, 0)<k) posj++;
while(poso<n&&val(posj+1, poso, 1)<k) poso++;
while(posi<n&&val(poso+1, posi, 2)<k) posi++;
if(val(i, posj, 0)==k && val(posj+1, poso, 1)==k && val(poso+1, posi, 2)==k){
ans=min(ans, posi-i+1-k*3);
}
}
if(ans==n+1) cout<<-1;
else cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |