Submission #1326473

#TimeUsernameProblemLanguageResultExecution timeMemory
1326473joacruJJOOII 2 (JOI20_ho_t2)C++20
0 / 100
0 ms332 KiB
#include <bits/stdc++.h>

#define forn(i,n) for(int i=0;i<int(n);++i)
#define fort(i,n) for(int i=0;i<=int(n);++i)
#define fori(i,a,n) for(int i=a;i<int(n);++i)
#define forit(i,a,n) for(int i=a;i<=int(n);++i)
#define ALL(v) v.begin(),v.end()
#define SZ(v) (int)v.size()

#define DBG(a) cerr<<#a<<" = "<<(a)<<endl
#define DBGA(a) cerr<<#a<<" = "<<(a)<<", ";
#define DBG2(a,b) do{DBGA(a)DBG(b);}while(0)
#define DBG3(a,b,c) do{DBGA(a)DBGA(b)DBG(c);}while(0)
#define DBG4(a,b,c,d) do{DBGA(a)DBGA(b)DBGA(c)DBG(d);}while(0)

#define LINE cerr<<"===================================="<<endl

using namespace std;

template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
	os<<"[";
	forn(i,v.size()){
		if(i) os<<" ";
		os<<v[i];
	}
	os<<"]";
	return os;
}

typedef long long ll;
typedef long double ld;

int get(string &s, int k, char c){
	int ret = 0;
	bool add = 0;
	while(SZ(s) && k){
		if(s.back() != c){
			ret += add;
		} else{
			add = 1;
			--k;
		}
		s.pop_back();
	}
	if(k > 0) ret = -1;
	return ret;
}

void solve(){
	
	int n, k;
	string s;
	cin>>n>>k>>s;
	
	int x = get(s, k, 'I');
	if(x == -1){
		cout<<"-1\n";
		return;
	}
	
	reverse(ALL(s));
	int y = get(s, k, 'J');
	if(y == -1){
		cout<<"-1\n";
		return;
	}
	
	int z = 0;
	forn(i,SZ(s)){
		if(s[i] != 'O' || k == 0){
			++z;
		} else if(s[i] == 'O'){
			--k;
		}
	}
	
	int ans = x+y+z;
	if(k) ans = -1;
	cout<<ans<<"\n";
	
}

int main() {
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	#ifdef LOCAL
		assert(freopen("input.in", "r", stdin));
		//~ freopen("output.out", "w", stdout);
	#endif
	
	#ifdef LOCAL
	int tcs; cin>>tcs;
	while(tcs--)
	#endif
	solve();

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...