이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << " " <<
#define nl << "\n"
int dp[2][1<<18], pos[3][1<<18];
int n, k;
string s;
void calc(int x, char C){
int cnt = 0;
pos[x][0] = -1;
for(int i=0; i+1<n; ++i){
if(s[i]==C) pos[x][++cnt] = i;
if(cnt>=k) dp[x][i] = i-pos[x][cnt-k+1]+1-k;
else dp[x][i] = n+n;
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k >> s;
calc(0, 'J');
reverse(s.begin(), s.end());
calc(1, 'I');
int ans = n+n, cnt = 0;
for(int i=0; i<n; ++i){
if(s[i]=='O') pos[2][++cnt] = i;
if(cnt>=k){
int a = pos[2][cnt-k+1] ? dp[1][pos[2][cnt-k+1]-1] : n+n;
int b = i+1<n ? dp[0][n-2-i] : n+n;
ans = min(ans, a + b + i-pos[2][cnt-k+1]+1-k);
}
}
if(ans>=n) ans = -1;
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... |