Submission #1261863

#TimeUsernameProblemLanguageResultExecution timeMemory
1261863tminhJJOOII 2 (JOI20_ho_t2)C++20
100 / 100
13 ms3600 KiB
#include "bits/stdc++.h"
using namespace std;

#define task ""
#define ll long long
#define endl '\n'
#define fi first
#define se second
#define vall(a) (a).begin(), (a).end()
#define sze(a) (int)a.size()
#define pii pair<int, int>
#define pll pair<ll, ll>


#define ep emplace_back
#define pb push_back
#define pf push_front


const ll mod = 1e9 + 7;
const int N = 2e5 + 5;
const int oo = 1e9;

bool START;

int n, k, pre[N][3];
string s;
vector<int> adj;


int get(int l, int r, int type) {
	return pre[r][type] - pre[l - 1][type];
}

int mask(char ch) {
	if (ch == 'J') return 0;
	if (ch == 'O') return 1;
	return 2;
}

int cnp(int st, int type) {
	int l = st, r = n, ans = n + 1;
	while(l <= r) {
		int mid = (l + r) >> 1;
		if (get(st, mid, type) >= k) {
			ans = mid;
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	return ans;
}

inline void solve() {
	s = '%' + s;
	for (int i = 1; i <= n; ++i) {
		if (!mask(s[i])) adj.pb(i);
		++pre[i][mask(s[i])];
		for (int j = 0; j < 3; ++j) pre[i][j] += pre[i - 1][j];
	}
	int ans = oo;
	for (int st : adj) {
		int en = cnp(st, 0);
		if (en == n + 1) continue;
		en = cnp(en + 1, 1);
		if (en == n + 1) continue;
		en = cnp(en + 1, 2);
		if (en == n + 1) continue;
		ans = min(ans, (en - st + 1) - 3 * k);
	}
	cout << (ans == oo ? -1 : ans);
	
}


inline void input() {
    cin >> n >> k >> s;
    return solve();
}
bool END;

int main() {
    if(fopen(task ".inp", "r")) {
        freopen(task ".inp", "r", stdin);
        freopen(task ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    input();
    
    
    cerr << endl << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << 's' << endl;
    cerr << "Memory: " << fabs ((&END - &START)) / 1048576.0 << "MB\n";
    return 0;
}

Compilation message (stderr)

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         freopen(task ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |         freopen(task ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...