Submission #532126

#TimeUsernameProblemLanguageResultExecution timeMemory
532126Nanashi_PandaJJOOII 2 (JOI20_ho_t2)C++14
0 / 100
0 ms204 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define jizz ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define ll long long
using namespace std;
char str[200005];
ll s,t;
ll n, k;
ll ans = 0;
ll loc, cnt;
int main() {
	scanf("%lld%lld", &n, &k);
	scanf("%s", str);
	s = 0;
	t = n-1;
	while(str[s]!='J') s++;
//	printf("%lld %lld\n", s, t);
	while(str[t]!='I') t--;
	
	
	cnt = 0;
	ll st = s;
	ll jp = 0;
	while(str[st++]=='J') cnt++;
	if(cnt>=k){
		s = st-1;
		jp = 1;
	}
	
	cnt = 0;
	ll tt = t;
	ll tp = 0;
	while(str[tt--]=='I') cnt++;
	if(cnt>=k){
		t = tt+1;
		tp = 1;
	}
	
//	printf("%lld %lld %lld %lld\n", s, t, jp, tp);
	
	if(s<t){
		loc = s;
		cnt = 0;
		if(!jp){
	//		printf("J:");
			for(loc = s; loc<=t; loc++){
				if(str[loc] == 'J'){
	//				printf("%lld ", loc);
					cnt++;
					if(cnt == k){
						ans += loc-s+1-k;
						s = loc+1;
						break;
					}
				}
			}
		}
//		printf("\n");
		if(cnt < k && !jp){
			printf("-1\n");
		}else{
			cnt = 0;
//			printf("I:");
			if(!tp){
				for(loc = t; loc>=s; loc--){
					if(str[loc] == 'I'){
	//					printf("%lld ", loc);
						cnt++;
						if(cnt == k){
							ans += t-loc+1-k;
							t = loc-1;
							break;
						}
					}
				}
			}
//			printf("\n");
			if(cnt < k && !tp){
				printf("-1\n");
			}else{
				cnt = 0;
//				printf("O:");
				for(loc = s; loc<=t; loc++){
					if(str[loc] == 'O'){
//						printf("%lld ", loc);
						cnt++;
						if(cnt == k){
							break;
						}
					}
				}
//				printf("\n");
				if(cnt < k){
					printf("-1\n");
				}else{
					ans += t-s+1-k;
					printf("%lld\n", ans);
				}
			}
		}
	}else{
		printf("-1\n");
	}
	return 0;
}

Compilation message (stderr)

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:14:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |  scanf("%lld%lld", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:15:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |  scanf("%s", str);
      |  ~~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...