답안 #568738

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
568738 2022-05-26T06:49:01 Z MateGiorbelidze JJOOII 2 (JOI20_ho_t2) C++14
0 / 100
2000 ms 212 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define ll long long
#define ff first
#define sc second
#define pb push_back
#define in insert
ll a[200001],d[200001];
 
int32_t main () {
	//ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    	
    	ll n, mx; cin>>n>>mx;
    	
    	string s; cin>>s;
    	
    	ll x = -1 , y = -1 , z = -1;
    	
    	for (int i = n - 1; i >= 0; i--) {
    		
    		if (s[i] == 'J') {
    			a[i] = x;
    			x = i;
			}
			
			if (s[i] == 'O') {
    			a[i] = y;
    			y = i;
			}
			
			if (s[i] == 'I') {
    			a[i] = z;
    			z = i;
			}
			
		}
    	/*
    	for (int i = 0; i < n; i++)
    		cout<<a[i]<<" ";*/
		for (int i = 0; i < n; i++){
			if(d[i] != 0) continue;
    		int k = i , l = i , cnt = 1;
			 
			while (k != -1 && cnt <= mx) {
				cnt++;
				l = k;
				k = a[k];
			}
				//cout<<l<<" "; 
			int j = i; 
			
			while (l != -1){
				d[j] = l;
				j = a[j];
				l = a[l];
			}
			
			while (j != -1) {
				d[j] = -1;
				j = a[j];
			}
				 			 		
		}
    	
    	/*
    	for (int i = 0; i < n; i++){
    		
			cout<<d[i]<<" ";	 			
		}*/
		x = -1 ; y = -1 ; z = -1;
		for(int i = 0; i < n; i++) {
			if(s[i] =='J') {
				if (d[i] == -1) break;
				x = i;
				
				for(int j = d[i] + 1; j < n; j++) {
					if (s[j] == 'O') {
						if (d[j] == -1) break;
						y = j;
						
						for (int k = d[j] + 1; k < n; k++) {
							if (s[k] == 'I') {
								if (d[k] == -1) break;
								z = k;
								break;
							}
						}
						break;
					}
					
				}
				break;
			}
			
		}// cout<<x<<" "<<y<<" "<<z;
		if (x == -1 || y == -1 || z == -1) {
			cout<<-1; return 0;
		}
		
		ll mn = d[z] - x + 1 - 3 * mx;// cout<<mn;
		
		while (d[x] != -1) {
			
			x = a[x];
			if (x == -1 || d[x] == -1) break;
			while (d[x] > y && d[y] != -1) {
				y = a[y];
			}
			
			if(y == -1 || d[y] == -1) break;
			
			while(d[y] > z && d[z] != -1) {
				z = a[z];
			}
			
			if (z == -1 && d[z] == -1) break;
			
			mn = min(mn , d[z] - x + 1 - 3 * mx);
			
			
		}

		cout<<mn;
    	
    
}

Compilation message

ho_t2.cpp: In function 'int32_t main()':
ho_t2.cpp:119:22: warning: array subscript -1 is below array bounds of 'long long int [200001]' [-Warray-bounds]
  119 |    if (z == -1 && d[z] == -1) break;
      |                   ~~~^
ho_t2.cpp:10:14: note: while referencing 'd'
   10 | ll a[200001],d[200001];
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2072 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2072 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2072 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -