이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
// costJ(i), costI(i)
// sol = min_{i, j} (costJ[i] -i - K) + (costI[j] + j-1)
signed main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int N, K;
string s;
cin >> N >> K >> s;
vector<int> costJ(N, 1e18), costI(N, 1e18);
vector<int> nbOcc(N);
for (int i(0); i < N; ++i)
{
if (i) nbOcc[i] += nbOcc[i-1];
nbOcc[i] += s[i] == 'O';
}
queue<int> q;
for (int i(0); i < N; ++i)
{
if (s[i] == 'J')
q.push(i);
while ((int)q.size() > K) q.pop();
if ((int)q.size() == K)
{
int lst = q.front();
costJ[i] = (i - lst +1 - K) - i - K;
}
}
while (!q.empty()) q.pop();
for (int i(N-1); i >= 0; --i)
{
if (s[i] == 'I')
q.push(i);
while ((int)q.size() > K) q.pop();
if ((int)q.size() == K)
{
int lst = q.front();
costI[i] = (lst - i + 1 - K) + i - 1;
}
if (i < N-1)
costI[i]= min(costI[i], costI[i+1]);
}
int sol(1e18);
for (int deb(0); deb < N-1; ++deb)
{
if (nbOcc[N-2] - nbOcc[deb] < K)
continue;
int lo(deb+1), up(N-1);
while (lo < up)
{
int mid = (lo + up) / 2;
if (nbOcc[mid-1] - nbOcc[deb] < K)
lo = mid+1;
else
up = mid;
}
int fin = lo;
if (fin < N)
sol = min(sol, costJ[deb] + costI[fin]);
}
if (sol == 1e18)
sol = -1;
cout << sol << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |