Submission #781621

# Submission time Handle Problem Language Result Execution time Memory
781621 2023-07-13T08:43:50 Z vjudge1 JJOOII 2 (JOI20_ho_t2) C++17
100 / 100
10 ms 3040 KB
#include<bits/stdc++.h>
using namespace std;
#define lalala ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define ll long long
#define pb push_back
#define N 2000005
int owo[N];
int main(){
	lalala;
	int n,k;cin>>n>>k;
	string str;cin>>str;
	vector<int>J,I,bir,iki;
	for(int i=0;i<n;i++){
		owo[i+1]+=owo[i];
		if(str[i]=='O')owo[i+1]++;
		if(str[i]=='J')J.pb(i+1);
		if(str[i]=='I')I.pb(i+1);
	}
	if(J.size()<k||I.size()<k){
		cout<<-1<<endl;
		return 0;
	}
	int why=0,neden=0;
	for(int i=1;i<k;i++){
		why+=J[i]-J[i-1]-1;
		neden+=I[i]-I[i-1]-1;
	}
	bir.pb(why);
	iki.pb(neden);
	for(int i=1;i<J.size()-k+1;i++){
		why-=(J[i]-J[i-1]-1);
		why+=(J[i+k-1]-J[i+k-2]-1);
		bir.pb(why);
	}
	for(int i=1;i<I.size()-k+1;i++){
		neden-=(I[i]-I[i-1]-1);
		neden+=(I[i+k-1]-I[i+k-2]-1);
		iki.pb(neden);
	}
	int ans=1000000000;
	for(int i=0;i<bir.size();i++){
		int yer=J[i],son=J[i+k-1];
		int cur=bir[i];
		int l=upper_bound(I.begin(),I.end(),son)-I.begin(), r=I.size()-1;


		//cout<<yer<<" "<<son<<" "<<l<<" "<<r<<" "<<I[l]<<" "<<i<<" "<<i+k-1<<endl;
		if(l==I.size())continue;
		while(l<r){
			int mid=(l+r)/2;
			int m=I[mid];
			//cout<<l<<" a "<<r<<" "<<mid<<" "<<m<<" "<<owo[m]<<endl;
			if(owo[m]-owo[son]>=k){
				r=mid;
			}
			else l=mid+1;
		}

		if(l>=iki.size()||owo[I[l]]-owo[son]<k)continue;
		cur+=iki[l];
		cur+=I[l]-son-1-k;

		//cout<<yer<<" "<<son<<" "<<l<<" "<<I[l]<<" "<<i<<" "<<i+k-1<<endl;
		//cout<<bir[i]<<" "<<iki[l]<<" "<<I[l]-son-1-k<<" "<<cur<<endl;
		ans=min(ans,cur);
	}
	if(ans==1000000000)ans=-1;
	cout<<ans<<endl;

}

Compilation message

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:20:13: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   20 |  if(J.size()<k||I.size()<k){
      |     ~~~~~~~~^~
ho_t2.cpp:20:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   20 |  if(J.size()<k||I.size()<k){
      |                 ~~~~~~~~^~
ho_t2.cpp:31:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |  for(int i=1;i<J.size()-k+1;i++){
      |              ~^~~~~~~~~~~~~
ho_t2.cpp:36:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |  for(int i=1;i<I.size()-k+1;i++){
      |              ~^~~~~~~~~~~~~
ho_t2.cpp:42:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |  for(int i=0;i<bir.size();i++){
      |              ~^~~~~~~~~~~
ho_t2.cpp:49:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |   if(l==I.size())continue;
      |      ~^~~~~~~~~~
ho_t2.cpp:60:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |   if(l>=iki.size()||owo[I[l]]-owo[son]<k)continue;
      |      ~^~~~~~~~~~~~
ho_t2.cpp:43:7: warning: unused variable 'yer' [-Wunused-variable]
   43 |   int yer=J[i],son=J[i+k-1];
      |       ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 324 KB Output is correct
22 Correct 1 ms 328 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 324 KB Output is correct
22 Correct 1 ms 328 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 324 KB Output is correct
36 Correct 8 ms 2548 KB Output is correct
37 Correct 9 ms 3040 KB Output is correct
38 Correct 9 ms 3028 KB Output is correct
39 Correct 9 ms 2648 KB Output is correct
40 Correct 9 ms 2592 KB Output is correct
41 Correct 8 ms 2644 KB Output is correct
42 Correct 10 ms 2984 KB Output is correct
43 Correct 5 ms 1728 KB Output is correct
44 Correct 7 ms 2092 KB Output is correct
45 Correct 7 ms 2596 KB Output is correct
46 Correct 7 ms 2624 KB Output is correct
47 Correct 7 ms 2592 KB Output is correct
48 Correct 7 ms 2644 KB Output is correct
49 Correct 5 ms 1800 KB Output is correct
50 Correct 7 ms 2644 KB Output is correct
51 Correct 7 ms 2624 KB Output is correct
52 Correct 4 ms 2080 KB Output is correct
53 Correct 4 ms 2396 KB Output is correct
54 Correct 5 ms 3020 KB Output is correct
55 Correct 3 ms 2516 KB Output is correct
56 Correct 2 ms 2524 KB Output is correct