// And you curse yourself for things you never done
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 5010, mod = 1e9 + 7, inf = 1e9 + 10;
int nxt[maxn][10], lst[10];
map<pii, int> dp;
vector<int> poses;
int solve(int i, int j){
if(i >= sz(poses))
return 0;
if(dp.count({i, j}))
return dp[{i, j}];
int ans = inf;
if(j >= poses[i])
ans = j - poses[i] + solve( upper_bound(poses.begin(), poses.end(), j) - poses.begin(), poses[i] );
int pt = lower_bound(nxt[j], nxt[j] + 10, poses[i]) - nxt[j];
for(int w = 8 ; w >= 0 && nxt[j][w] != j && w >= pt-1; w--)
ans = min(ans, 2 + solve(i, nxt[j][w]));
return dp[{i, j}] = ans;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();
int n;
cin >> n;
string _s, s;
cin >> _s;
int ans = 0;
for(int i = 0; i < n; i++){
if(_s[i] == 'e')
ans+= 2;
if(_s[i] == 'e' && _s[i + 1] != 'e')
poses.PB(sz(s));
if(_s[i] != 'e')
s+= _s[i];
}
memset(lst, -1, sizeof lst);
memset(nxt, -1, sizeof nxt);
n = sz(s);
for(int i = n-1; i >= 0; i--){
for(int j = 0; j < 9; j++)
nxt[i][j] = (lst[j] == -1 ? i : lst[j]);
sort(nxt[i], nxt[i] + 9);
nxt[i][9] = inf;
int id = (s[i] - 'a') - (s[i] > 'e');
lst[id] = i;
}
ans+= solve(0, 0);
return cout << ans << "\n", 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
2424 KB |
Output is correct |
2 |
Correct |
32 ms |
2944 KB |
Output is correct |
3 |
Correct |
15 ms |
1792 KB |
Output is correct |
4 |
Correct |
37 ms |
2684 KB |
Output is correct |
5 |
Correct |
46 ms |
2936 KB |
Output is correct |
6 |
Correct |
75 ms |
4196 KB |
Output is correct |
7 |
Correct |
58 ms |
3832 KB |
Output is correct |
8 |
Correct |
0 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
0 ms |
512 KB |
Output is correct |
13 |
Correct |
33 ms |
2424 KB |
Output is correct |
14 |
Correct |
31 ms |
2936 KB |
Output is correct |
15 |
Correct |
15 ms |
1792 KB |
Output is correct |
16 |
Correct |
30 ms |
2936 KB |
Output is correct |
17 |
Correct |
45 ms |
2808 KB |
Output is correct |
18 |
Correct |
33 ms |
2552 KB |
Output is correct |
19 |
Correct |
28 ms |
2432 KB |
Output is correct |
20 |
Correct |
33 ms |
2552 KB |
Output is correct |
21 |
Correct |
38 ms |
2684 KB |
Output is correct |
22 |
Correct |
47 ms |
2940 KB |
Output is correct |
23 |
Correct |
23 ms |
2048 KB |
Output is correct |
24 |
Correct |
34 ms |
2168 KB |
Output is correct |
25 |
Correct |
28 ms |
2048 KB |
Output is correct |
26 |
Correct |
46 ms |
2516 KB |
Output is correct |
27 |
Correct |
56 ms |
3448 KB |
Output is correct |
28 |
Correct |
74 ms |
4216 KB |
Output is correct |
29 |
Correct |
56 ms |
3832 KB |
Output is correct |
30 |
Correct |
36 ms |
2680 KB |
Output is correct |
31 |
Correct |
46 ms |
2272 KB |
Output is correct |
32 |
Correct |
35 ms |
2532 KB |
Output is correct |
33 |
Correct |
50 ms |
2624 KB |
Output is correct |
34 |
Correct |
47 ms |
3064 KB |
Output is correct |
35 |
Correct |
57 ms |
3448 KB |
Output is correct |
36 |
Correct |
61 ms |
3832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2100 ms |
90660 KB |
Time limit exceeded |
2 |
Execution timed out |
2080 ms |
68296 KB |
Time limit exceeded |
3 |
Correct |
1616 ms |
62364 KB |
Output is correct |
4 |
Execution timed out |
2097 ms |
85112 KB |
Time limit exceeded |
5 |
Execution timed out |
2099 ms |
73592 KB |
Time limit exceeded |
6 |
Execution timed out |
2096 ms |
70824 KB |
Time limit exceeded |
7 |
Execution timed out |
2102 ms |
84292 KB |
Time limit exceeded |
8 |
Execution timed out |
2101 ms |
87416 KB |
Time limit exceeded |
9 |
Execution timed out |
2098 ms |
76764 KB |
Time limit exceeded |
10 |
Execution timed out |
2085 ms |
69592 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
1408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
2 ms |
1536 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
2 ms |
1536 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
3 ms |
1664 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
3 ms |
1664 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
2 ms |
1536 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
2 ms |
1536 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
3 ms |
1636 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
3 ms |
1536 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Runtime error |
3 ms |
1596 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
11 |
Runtime error |
2 ms |
1688 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Runtime error |
3 ms |
1664 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
13 |
Runtime error |
2 ms |
1664 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
14 |
Runtime error |
3 ms |
1664 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
15 |
Runtime error |
2 ms |
1664 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
16 |
Runtime error |
2 ms |
1408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
17 |
Runtime error |
2 ms |
1408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
18 |
Runtime error |
3 ms |
1408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
19 |
Runtime error |
4 ms |
1536 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
20 |
Runtime error |
2 ms |
1536 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |