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;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
int get(vector <int> a, int i, int k){
int ind = upper_bound(a.begin(), a.end(), i) - a.begin();
ind += k - 1;
if (ind >= a.size()) return -1;
// cout << "RETURNING " << ind << "\n";
return a[ind];
}
void Solve()
{
int n, k; cin >> n >> k;
vector <int> a[3];
string s; cin >> s;
for (int i = 1; i <= n; i++){
if (s[i - 1] == 'J') a[0].push_back(i);
else if (s[i - 1] == 'O') a[1].push_back(i);
else a[2].push_back(i);
}
int ans = INF;
for (int i = 0; i < n; i++){
//we will leave [1, i] and start from i + 1
// cout << "DOING " << i << "\n";
int g = get(a[0], i, k); //get position of ending if we start at > x
if (g != -1) g = get(a[1], g, k);
if (g != -1) g = get(a[2], g, k);
if (g != -1) ans = min(ans, g - i - 3 * k);
}
if (ans == INF) cout << -1 << "\n";
else cout << ans << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message (stderr)
ho_t2.cpp: In function 'long long int get(std::vector<long long int>, long long int, long long int)':
ho_t2.cpp:14:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | if (ind >= a.size()) return -1;
| ~~~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |