# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
797271 | n3rm1n | JJOOII 2 (JOI20_ho_t2) | C++17 | 8 ms | 3284 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int MAXN = 2e5 + 10;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int n, k;
char s[MAXN];
void read()
{
cin >> n >> k;
for (int i = 1; i <= n; ++ i)
cin >> s[i];
}
int dp0[MAXN], dp1[MAXN];
int cnt[MAXN];
void precompute()
{
deque < int > q;
for (int i = n; i >= 1; -- i)
{
if(s[i] == 'J')q.push_back(i);
if(q.size() > k)q.pop_front();
if(q.size() < k)dp0[i] = n+1;
else dp0[i] = q.front();
}
q.clear();
for (int i = 1; i <= n; ++ i)
{
if(s[i] == 'I')q.push_back(i);
if(q.size() > k)q.pop_front();
if(q.size() < k)dp1[i] = 0;
else dp1[i] = q.front();
}
for (int i = 1; i <= n; ++ i)
{
cnt[i] = cnt[i-1];
if(s[i] == 'O')cnt[i] ++;
}
/* for (int i = 1; i <= n; ++ i)
cout << dp0[i] << " ";
cout << endl;
for (int i = 1; i <= n; ++ i)
cout << dp1[i] << " ";
cout << endl; */
}
bool check(int x)
{
for (int i = 0; i <= x; ++ i) /// how much we delete from the front
{
int lastj = dp0[i+1];
int firsti = dp1[n - (x - i)];
if(lastj > firsti)continue;
if(cnt[firsti - 1] - cnt[lastj] >= k)return true;
}
return false;
}
void solve()
{
int left = 0, right = n - 3*k, mid, ans = -1;
while(left <= right)
{
mid = (left + right) / 2;
if(check(mid))
{
ans = mid;
left = mid + 1;
}
else right = mid - 1;
}
if(ans == -1)cout << -1 << endl;
else cout << n - 3*k - ans << endl;
}
int main()
{
speed();
read();
precompute();
solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |