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>
using namespace std;
const int mxN = 1000005;
int n;
string str;
bool check(int x)
{
int j = 0, lastI;
for (int i = n - 1; i >= 0; i--){
if (str[i] == 'I' && j < x) {
lastI = i, j++;
}
}
if (j < x) return false;
int ret = 0, IJcnt = 0;
for (int i = 0; i < n; i++){
if (str[i] == 'J' || (str[i] == 'I' && i < lastI)) IJcnt++;
if (str[i] == 'O' && IJcnt) {ret++, IJcnt--;}
}
return ret >= x;
}
int main()
{
cin >> n >> str;
int low = 0, high = n / 3, mid, ans = 0;
while (low <= high){
mid = (low + high) / 2;
if (check(mid)) {
ans = mid;
low = mid + 1;
}
else high = mid - 1;
}
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
2014_ho_t1.cpp: In function 'bool check(int)':
2014_ho_t1.cpp:19:45: warning: 'lastI' may be used uninitialized in this function [-Wmaybe-uninitialized]
19 | if (str[i] == 'J' || (str[i] == 'I' && i < lastI)) IJcnt++;
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |