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>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()
// #define int long long
// #define int unsigned long long
// #define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>
void open_file(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
// const ll mod = 1e9 + 7;
// const ll mod = 998244353;
const ll inf = 1e9;
const ll biginf = 1e18;
// const int maxN;
void solve() {
int n, k; string s;
cin >> n >> k >> s;
int arr[n + 1];
for (int i = 1; i <= n; i++) {
if (s[i - 1] == 'J') arr[i] = 0;
else if (s[i - 1] == 'O') arr[i] = 1;
else arr[i] = 2;
} deque<int> dq[3];
int nxt[n + 1][3];
for (int i = 1; i <= n; i++)
for (int j = 0; j < 3; j++) nxt[i][j] = -1;
for (int i = n; i >= 1; i--) {
dq[arr[i]].push_back(i);
if (dq[arr[i]].size() > k)
dq[arr[i]].pop_front();
for (int j = 0; j < 3; j++) {
if (dq[j].size() < k) continue;
nxt[i][j] = dq[j].front();
}
} int ans = inf;
for (int i = 1; i <= n; i++) {
int j = nxt[i][0];
if (j == -1) continue;
j = nxt[j][1];
if (j == -1) continue;
j = nxt[j][2];
if (j == -1) continue;
ans = min(ans, j - i + 1);
} if (ans == inf) {
cout << -1; return;
} cout << ans - 3 * k;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}
Compilation message (stderr)
ho_t2.cpp: In function 'void solve()':
ho_t2.cpp:51:31: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
51 | if (dq[arr[i]].size() > k)
| ~~~~~~~~~~~~~~~~~~^~~
ho_t2.cpp:54:30: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
54 | if (dq[j].size() < k) continue;
| ~~~~~~~~~~~~~^~~
ho_t2.cpp: In function 'void open_file(std::string)':
ho_t2.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | freopen((filename + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen((filename + ".out").c_str(), "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... |