제출 #1309693

#제출 시각아이디문제언어결과실행 시간메모리
1309693nicolo_010JJOOII 2 (JOI20_ho_t2)C++20
0 / 100
2 ms576 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
const int MOD = 1e9+7;

vector<vector<int>> lift;

int jump(int n, int k) {
	for (int i=0; i<21; i++) {
		if (k&(1<<i)) {
			n = lift[n][i];
		}
	}
	return n;
}
#define cerr if(0) cerr

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n, k; cin >> n >> k;
	string s; cin >> s;
	lift.assign(n, vector<int>(21, 0));
	vector<int> nxj(n, 0), nxo(n, 0), nxi(n, 0);
	int nxtj=0, nxto=0, nxti=0;
	for (int i=n-1; i>=0; i--) {
		nxj[i] = nxtj;
		nxi[i] = nxti;
		nxo[i] = nxto;
		if (s[i] == 'J') {
			nxtj = i;
		}
		else if (s[i] == 'O') {
			nxto = i;
		}
		else {
			nxti = i;
		}
	}
	for (int i=0; i<n; i++) {
		if (s[i]=='J') {
			lift[i][0] = nxj[i];
		}
		else if (s[i] == 'O') {
			lift[i][0] = nxo[i];
		}
		else {
			lift[i][0] = nxi[i];
		}
	}
	for (int j=1; j<21; j++) {
		for (int i=0; i<n; i++) {
			lift[i][j] = lift[lift[i][j-1]][j-1];
		}
	}
	int mn=1e9;
	for (int i=0; i<n; i++) {
		if (s[i] == 'J') {
			cerr << i << ": " << endl;
			int lastj = jump(i, k-1);
			cerr << lastj << endl;
			if (lastj < i) continue;
			int frsto = nxo[lastj];
			cerr << frsto << endl;
			if (frsto < i) continue;
			int lasto = jump(frsto, k-1);
			cerr << lasto << endl;
			if (lasto<i) continue;
			int firsti = nxi[lasto];
			cerr << firsti << endl;
			if (firsti<i) continue;
			int lasti = jump(firsti, k-1);
			cerr << lasti << endl;
			if (lasti<i) continue;
			mn = min(mn, lasti-i+1-3*k);
		}
	}
	cout << (mn==1e9 ? -1 : mn) << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...