답안 #568614

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
568614 2022-05-25T19:52:21 Z MateGiorbelidze JJOOII 2 (JOI20_ho_t2) C++17
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 o = 1; //cin>>o;
    
    while (o--) {
    	
    	ll n, mx; cin>>n>>mx;
    	
    	string s; cin>>s;
    	
    	for (int i = 0; i < n; i++){
    		char ch = s[i];
    		a[i] = -1;
    		for (int j = i + 1; j < n; j++)
				if (s[j] == ch) {
					
					a[i] = j; break;
					
				}   
			//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]<<" ";	 			
		}*/
		ll 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 (d[x] == -1) break;
			while (d[x] > y && d[y] != -1) {
				y = a[y];
			}
			
			if(d[y] == -1) break;
			
			while(d[y] > z && d[z] != -1) {
				z = a[z];
			}
			
			if (d[z] == -1) break;
			
			mn = min(mn , d[z] - x + 1 - 3 * mx);
			
			
		}
		
		cout<<mn;
    	
	}
    
}
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2058 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2058 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2058 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -