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;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 200005
int N, K;
string s;
int L[MAX], R[MAX], M[MAX];
void process(void){
cin >> N >> K >> s;
s = '@' + s;
deque<int> l;
int dis = 0;
for (int i = 1; i <= N; ++i){
if(s[i] != 'J'){
L[i] = INF; continue;
}
if(l.size()) dis += i - l.back() - 1;
l.push_back(i);
if(l.size() > K){
assert(l.size() >= 2);
dis -= l[1] - l[0] - 1;
l.pop_front();
}
L[i] = (l.size() == K ? dis : INF);
}
deque<int> m;
dis = 0;
for (int i = 1; i <= N; ++i){
if (s[i] != 'O'){
M[i] = INF; continue;
}
if (m.size()) dis += i - m.back() - 1;
m.push_back(i);
if(m.size() > K){
assert(m.size() >= 2);
dis -= m[1] - m[0] - 1;
m.pop_front();
}
M[i] = (m.size() == K ? dis : INF);;
}
deque<int> r;
dis = 0;
for (int i = N; i >= 1; --i){
if(s[i] != 'I'){
R[i] = INF; continue;
}
if(r.size()) dis += r.back() - i - 1;
r.push_back(i);
if(r.size() > K){
assert(r.size() >= 2);
dis -= r[0] - r[1] - 1;
r.pop_front();
}
R[i] = (r.size() == K ? dis : INF);
}
R[N + 1] = L[0] = INF;
int ans = INF;
for (int i = 1; i <= N; ++i){
if(M[i] >= INF) continue;
int pre_pos = i - M[i] - K;
if(pre_pos >= 0){
minimize(ans, L[pre_pos] + M[i] + R[i + 1]);
}
}
cout << (ans >= INF ? -1 : ans);
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
Compilation message (stderr)
ho_t2.cpp: In function 'void process()':
ho_t2.cpp:56:21: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
56 | if(l.size() > K){
| ~~~~~~~~~^~~
ho_t2.cpp:61:26: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
61 | L[i] = (l.size() == K ? dis : INF);
| ~~~~~~~~~^~~~
ho_t2.cpp:72:21: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
72 | if(m.size() > K){
| ~~~~~~~~~^~~
ho_t2.cpp:77:26: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
77 | M[i] = (m.size() == K ? dis : INF);;
| ~~~~~~~~~^~~~
ho_t2.cpp:88:21: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
88 | if(r.size() > K){
| ~~~~~~~~~^~~
ho_t2.cpp:93:26: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
93 | R[i] = (r.size() == K ? dis : INF);
| ~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |