Submission #122185

# Submission time Handle Problem Language Result Execution time Memory
122185 2019-06-27T19:34:23 Z Adhyyan1252 Vim (BOI13_vim) C++11
27.7778 / 100
49 ms 15488 KB
#include<bits/stdc++.h>

using namespace std;
#define INF 1e6
int main(){
	int n; cin>>n;
	vector<int> a(n+1);
	for(int i = 1; i <= n; i++){
		char c; cin>>c;
		a[i] = c - 'a';
	}
	
	vector<vector<int> > next(n+2, vector<int>(10, INF));
	for(int i = n; i > 0; i--){
		next[i] = next[i+1];
		next[i][a[i]] = i;
	}
	
	vector<vector<int> > pref(n+2, vector<int>(10, 0));
	for(int i = 1; i <= n; i++){
		pref[i] = pref[i-1];
		pref[i][a[i]]++;
	}
	
	vector<vector<int> > dp(n+2, vector<int>(10, INF));
	for(int i = 0; i < 10; i++){
		dp[n+1][i] = 0;
	}
	for(int i = n; i > 0; i--){
		for(int j = 0; j < 10; j++){
			
		int cur = next[i][j]; //cursor is here. need to delete all from i to n
		if(cur == INF) continue;
		//first come back to the leftest e
		if(j == 4){
			continue;
		}
		int indx = next[i][4];

		if(indx == INF){ //no e's after i
			dp[i][j] = 0;
		}else if(indx > cur){
			//this section has no e's so just move forward by exactly one fk
			for(int k = 0; k < 10; k++){
				if(k == 4) continue;
				dp[i][j] = min(dp[i][j], 2 + dp[cur+1][k]); //cursor moves to next[cur+1][k]
			}
		}else if(next[cur+1][4] == INF){
			int cost = cur - indx + pref[cur][4] - pref[indx-1][4];
			dp[i][j] = cost;
		}else{
			
			//first go left to reach leftest e. 
			int cost = cur - indx + pref[cur][4] - pref[indx-1][4];
			//jump from indx+1 to next[cur+1][k]
			
			//find next element, after
			int after = cur;
			for(int k = 0; k < 10; k++){
				if(k == 4) continue;
				after = min(after, next[indx][k]);
			}
			
			int oc = INF;
			for(int k = 0; k < 10; k++){
				if(k == 4) continue;
				if(next[cur+1][k] == INF) continue;
				oc = min(oc, dp[cur+1][k] + 2*(pref[next[cur+1][k]][k] - pref[after][k]));
			}
			dp[i][j] = min((int)INF, oc + cost);
		}	
	}
//		cout<<"DP "<<i<<" : ";
//		for(int j = 0; j < 10; j++){
//			cout<<dp[i][j]<<"\t";
//		}
//		cout<<endl;
	}
	int ans = dp[1][a[1]];
//	for(int i = 0; i < 10; i++){
//		ans = min(ans, dp[1][i]);
//	}
	cout<<ans<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Incorrect 3 ms 384 KB Output isn't correct
3 Incorrect 2 ms 384 KB Output isn't correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Incorrect 3 ms 512 KB Output isn't correct
7 Incorrect 2 ms 512 KB Output isn't correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Incorrect 2 ms 384 KB Output isn't correct
15 Incorrect 2 ms 384 KB Output isn't correct
16 Incorrect 2 ms 384 KB Output isn't correct
17 Correct 2 ms 384 KB Output is correct
18 Incorrect 2 ms 384 KB Output isn't correct
19 Incorrect 2 ms 384 KB Output isn't correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Incorrect 2 ms 512 KB Output isn't correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Incorrect 3 ms 384 KB Output isn't correct
29 Incorrect 2 ms 400 KB Output isn't correct
30 Incorrect 2 ms 384 KB Output isn't correct
31 Correct 3 ms 384 KB Output is correct
32 Incorrect 3 ms 512 KB Output isn't correct
33 Correct 2 ms 384 KB Output is correct
34 Correct 2 ms 384 KB Output is correct
35 Incorrect 3 ms 384 KB Output isn't correct
36 Incorrect 2 ms 512 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 1408 KB Output isn't correct
2 Incorrect 6 ms 1408 KB Output isn't correct
3 Incorrect 4 ms 896 KB Output isn't correct
4 Incorrect 5 ms 1408 KB Output isn't correct
5 Incorrect 6 ms 1408 KB Output isn't correct
6 Incorrect 6 ms 1408 KB Output isn't correct
7 Incorrect 5 ms 1408 KB Output isn't correct
8 Incorrect 6 ms 1408 KB Output isn't correct
9 Incorrect 5 ms 1408 KB Output isn't correct
10 Incorrect 5 ms 1408 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 39 ms 13440 KB Output isn't correct
2 Incorrect 36 ms 13164 KB Output isn't correct
3 Incorrect 40 ms 13440 KB Output isn't correct
4 Incorrect 42 ms 15488 KB Output isn't correct
5 Incorrect 48 ms 15488 KB Output isn't correct
6 Incorrect 42 ms 13660 KB Output isn't correct
7 Incorrect 42 ms 14072 KB Output isn't correct
8 Incorrect 44 ms 14456 KB Output isn't correct
9 Incorrect 44 ms 14328 KB Output isn't correct
10 Incorrect 47 ms 14456 KB Output isn't correct
11 Incorrect 44 ms 15488 KB Output isn't correct
12 Incorrect 49 ms 15480 KB Output isn't correct
13 Incorrect 43 ms 15140 KB Output isn't correct
14 Incorrect 42 ms 15224 KB Output isn't correct
15 Incorrect 46 ms 15224 KB Output isn't correct
16 Incorrect 35 ms 13184 KB Output isn't correct
17 Incorrect 41 ms 13440 KB Output isn't correct
18 Incorrect 40 ms 13500 KB Output isn't correct
19 Incorrect 37 ms 13056 KB Output isn't correct
20 Incorrect 42 ms 13304 KB Output isn't correct