#include<bits/stdc++.h>
using namespace std;
#define INF 1e7
int main(){
int n; cin>>n;
vector<int> a(n+2);
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Incorrect |
2 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 |
2 ms |
512 KB |
Output isn't correct |
7 |
Incorrect |
2 ms |
512 KB |
Output isn't correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
256 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 |
3 ms |
512 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 |
3 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 |
2 ms |
384 KB |
Output is correct |
23 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
3 ms |
384 KB |
Output is correct |
28 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
29 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
30 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Incorrect |
2 ms |
384 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 |
2 ms |
384 KB |
Output isn't correct |
36 |
Incorrect |
2 ms |
356 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
1408 KB |
Output isn't correct |
2 |
Incorrect |
5 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 |
5 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 |
5 ms |
1408 KB |
Output isn't correct |
9 |
Incorrect |
5 ms |
1536 KB |
Output isn't correct |
10 |
Incorrect |
5 ms |
1408 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
42 ms |
13432 KB |
Output isn't correct |
2 |
Incorrect |
38 ms |
13132 KB |
Output isn't correct |
3 |
Incorrect |
39 ms |
13440 KB |
Output isn't correct |
4 |
Incorrect |
45 ms |
15488 KB |
Output isn't correct |
5 |
Incorrect |
48 ms |
15360 KB |
Output isn't correct |
6 |
Incorrect |
44 ms |
13568 KB |
Output isn't correct |
7 |
Incorrect |
45 ms |
14080 KB |
Output isn't correct |
8 |
Incorrect |
46 ms |
14336 KB |
Output isn't correct |
9 |
Incorrect |
47 ms |
14208 KB |
Output isn't correct |
10 |
Incorrect |
45 ms |
14336 KB |
Output isn't correct |
11 |
Incorrect |
42 ms |
15480 KB |
Output isn't correct |
12 |
Incorrect |
48 ms |
15364 KB |
Output isn't correct |
13 |
Incorrect |
44 ms |
14980 KB |
Output isn't correct |
14 |
Incorrect |
42 ms |
15104 KB |
Output isn't correct |
15 |
Incorrect |
50 ms |
15480 KB |
Output isn't correct |
16 |
Incorrect |
37 ms |
13184 KB |
Output isn't correct |
17 |
Incorrect |
41 ms |
13312 KB |
Output isn't correct |
18 |
Incorrect |
41 ms |
13432 KB |
Output isn't correct |
19 |
Incorrect |
37 ms |
13056 KB |
Output isn't correct |
20 |
Incorrect |
40 ms |
13184 KB |
Output isn't correct |