This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
//#define int long long
int n, k; bool del[MAXN];
string s;
vector<int> J;
vector<int> O;
vector<int> I;
int pre[MAXN], suf[MAXN];
void Solve() {
cin >> n >> k >> s;
s = '$' + s;
For(i, 1, n){
if(s[i] == 'J') J.push_back(i);
if(s[i] == 'O') O.push_back(i);
if(s[i] == 'I') I.push_back(i);
}
For(i, 1, J.size() - 1){
pre[i] = pre[i - 1] + max(0, J[i] - 1 - (J[i - 1] + 1) + 1);
}
Ford(i, I.size() - 2, 0){
suf[i] = suf[i + 1] + max(0, I[i + 1] - 1 - (I[i] + 1) + 1);
}
int ans = inf, sum = 0;
if(O.size() < k) {
cout << -1 << '\n'; return;
}
For(i, 1, k - 1) sum += O[i] - 1 - (O[i - 1] + 1) + 1;
// cout << sum << '\n';
for (int i = k - 1; i <= O.size() - 1; ++i){
if(i != k - 1){
sum += O[i] - 1 - (O[i - 1] + 1) + 1;
sum -= O[i - k + 1] - 1 - (O[i - k] + 1) + 1;
}
int L = O[i - k + 1], R = O[i];
int r = lower_bound(all(I), R) - I.begin();
if(r + k - 1 > I.size() - 1) continue;
int lo = -1, hi = J.size();
while(hi - lo > 1){
int mid = lo + hi >> 1;
if(J[mid] <= L) lo = mid;
else hi = mid;
}
int l = lo;
if(l - k + 1 < 0) continue;
int cur = sum;
cur += I[r] - 1 - (O[i] + 1) + 1;
cur += O[i - k + 1] - 1 - (J[l] + 1) + 1;
cur += pre[l] - pre[l - k + 1];
cur += suf[r] - suf[r + k - 1];
minimize(ans, cur);
}
if(ans == inf) ans = -1;
cout << ans << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(0);
if(fopen("JOI20_ho_t2.inp", "r")) {
freopen("JOI20_ho_t2.inp", "r", stdin);
freopen("JOI20_ho_t2.out", "w", stdout);
}
int t = 1;
// cin >> t;
for (int test = 1; test <= t; test++) {
Solve();
}
return 0;
}
Compilation message (stderr)
ho_t2.cpp: In function 'void Solve()':
ho_t2.cpp:10:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | #define For(i, a, b) for(int i = a; i <= b; ++i)
......
53 | For(i, 1, J.size() - 1){
| ~~~~~~~~~~~~~~~~~~
ho_t2.cpp:53:5: note: in expansion of macro 'For'
53 | For(i, 1, J.size() - 1){
| ^~~
ho_t2.cpp:60:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
60 | if(O.size() < k) {
| ~~~~~~~~~^~~
ho_t2.cpp:65:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int i = k - 1; i <= O.size() - 1; ++i){
| ~~^~~~~~~~~~~~~~~
ho_t2.cpp:72:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | if(r + k - 1 > I.size() - 1) continue;
| ~~~~~~~~~~^~~~~~~~~~~~~~
ho_t2.cpp:75:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
75 | int mid = lo + hi >> 1;
| ~~~^~~~
ho_t2.cpp: In function 'int32_t main()':
ho_t2.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen("JOI20_ho_t2.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen("JOI20_ho_t2.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |