제출 #1309695

#제출 시각아이디문제언어결과실행 시간메모리
1309695nicolo_010JJOOII 2 (JOI20_ho_t2)C++20
100 / 100
43 ms26468 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)) {
			if (lift[n][i] == -1) return -1;
			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, -1));
	vector<int> nxj(n, -1), nxo(n, -1), nxi(n, -1);
	int nxtj=-1, nxto=-1, nxti=-1;
	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++) {
			if (lift[i][j-1] == -1) {
				lift[i][j] = -1;
				continue;
			}
			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...