Submission #955173

#TimeUsernameProblemLanguageResultExecution timeMemory
955173jcelinJJOOII 2 (JOI20_ho_t2)C++14
100 / 100
13 ms1888 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<pll> vpll;

#define PB push_back
#define PF push_front<<
#define PPB pop_back
#define PPF pop_front
#define X first
#define Y second
#define MP make_pair
#define all(x) (x).begin(), (x).end()

const int mod = 1e9 + 7; //998244353;
const int inf = 1e9 + 7;
const ll INF = 1e18 + 7;
const int logo = 20;
const int MAXN = 1e6 + 7;
const int off = 1 << logo;
const int trsz = off << 1;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};

vi p[27];
int k;

//kti >= x
int kth(int id, int x){
	int it = lower_bound(all(p[id]), x) - p[id].begin() + k - 1;
	if(it >= (int)p[id].size()) return inf;
	return p[id][it];
}

void solve(){
	int n;
	cin >> n >> k;
	for(int i=1; i<=n; i++){
		char x;
		cin >> x;
		p[x - 'A'].PB(i);
	}
	
	int a = 'J' - 'A';
	int b = 'O' - 'A';
	int c = 'I' - 'A';
	
	int ans = inf;
	for(auto &l : p['J' - 'A']){
		int pr = kth(a, l);
		pr = kth(b, pr);
		pr = kth(c, pr);
		if(pr < inf) ans = min(ans, pr - l + 1);
	}
	
	if(ans == inf) cout << -1 << "\n";
	else cout << ans - 3 * k << "\n";
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int tt = 1;
	//cin >> tt;
	while(tt--) solve();
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...