// 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 = 70010, 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(nxt[ poses[i] ][8] + 10 < j)
return inf;
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 |
29 ms |
4472 KB |
Output is correct |
2 |
Correct |
15 ms |
4088 KB |
Output is correct |
3 |
Correct |
10 ms |
3712 KB |
Output is correct |
4 |
Correct |
17 ms |
3984 KB |
Output is correct |
5 |
Correct |
41 ms |
5112 KB |
Output is correct |
6 |
Correct |
18 ms |
3968 KB |
Output is correct |
7 |
Correct |
20 ms |
4096 KB |
Output is correct |
8 |
Correct |
2 ms |
3072 KB |
Output is correct |
9 |
Correct |
2 ms |
3072 KB |
Output is correct |
10 |
Correct |
3 ms |
3072 KB |
Output is correct |
11 |
Correct |
2 ms |
3072 KB |
Output is correct |
12 |
Correct |
2 ms |
3072 KB |
Output is correct |
13 |
Correct |
29 ms |
4472 KB |
Output is correct |
14 |
Correct |
14 ms |
4096 KB |
Output is correct |
15 |
Correct |
9 ms |
3840 KB |
Output is correct |
16 |
Correct |
14 ms |
3968 KB |
Output is correct |
17 |
Correct |
17 ms |
3816 KB |
Output is correct |
18 |
Correct |
13 ms |
3712 KB |
Output is correct |
19 |
Incorrect |
18 ms |
4224 KB |
Output isn't correct |
20 |
Correct |
21 ms |
4224 KB |
Output is correct |
21 |
Correct |
18 ms |
4088 KB |
Output is correct |
22 |
Correct |
41 ms |
5112 KB |
Output is correct |
23 |
Correct |
18 ms |
4088 KB |
Output is correct |
24 |
Correct |
21 ms |
4028 KB |
Output is correct |
25 |
Correct |
11 ms |
3584 KB |
Output is correct |
26 |
Correct |
8 ms |
3456 KB |
Output is correct |
27 |
Correct |
13 ms |
3712 KB |
Output is correct |
28 |
Correct |
18 ms |
3968 KB |
Output is correct |
29 |
Correct |
20 ms |
4088 KB |
Output is correct |
30 |
Correct |
17 ms |
3968 KB |
Output is correct |
31 |
Correct |
15 ms |
3712 KB |
Output is correct |
32 |
Correct |
9 ms |
3456 KB |
Output is correct |
33 |
Correct |
8 ms |
3456 KB |
Output is correct |
34 |
Correct |
15 ms |
3712 KB |
Output is correct |
35 |
Correct |
16 ms |
3968 KB |
Output is correct |
36 |
Correct |
20 ms |
4096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
11136 KB |
Output is correct |
2 |
Correct |
200 ms |
13176 KB |
Output is correct |
3 |
Correct |
126 ms |
9272 KB |
Output is correct |
4 |
Correct |
163 ms |
11128 KB |
Output is correct |
5 |
Correct |
810 ms |
33432 KB |
Output is correct |
6 |
Execution timed out |
2076 ms |
66968 KB |
Time limit exceeded |
7 |
Correct |
761 ms |
33144 KB |
Output is correct |
8 |
Correct |
258 ms |
14456 KB |
Output is correct |
9 |
Correct |
201 ms |
13248 KB |
Output is correct |
10 |
Incorrect |
272 ms |
17144 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2097 ms |
68264 KB |
Time limit exceeded |
2 |
Correct |
1096 ms |
59128 KB |
Output is correct |
3 |
Correct |
1895 ms |
86264 KB |
Output is correct |
4 |
Execution timed out |
2102 ms |
104824 KB |
Time limit exceeded |
5 |
Execution timed out |
2101 ms |
75128 KB |
Time limit exceeded |
6 |
Execution timed out |
2105 ms |
97656 KB |
Time limit exceeded |
7 |
Execution timed out |
2092 ms |
91788 KB |
Time limit exceeded |
8 |
Execution timed out |
2101 ms |
73976 KB |
Time limit exceeded |
9 |
Execution timed out |
2103 ms |
88824 KB |
Time limit exceeded |
10 |
Execution timed out |
2099 ms |
95464 KB |
Time limit exceeded |
11 |
Execution timed out |
2104 ms |
105464 KB |
Time limit exceeded |
12 |
Execution timed out |
2099 ms |
75384 KB |
Time limit exceeded |
13 |
Execution timed out |
2093 ms |
95272 KB |
Time limit exceeded |
14 |
Correct |
1345 ms |
66936 KB |
Output is correct |
15 |
Execution timed out |
2097 ms |
97208 KB |
Time limit exceeded |
16 |
Execution timed out |
2100 ms |
81884 KB |
Time limit exceeded |
17 |
Execution timed out |
2093 ms |
65108 KB |
Time limit exceeded |
18 |
Correct |
1895 ms |
86388 KB |
Output is correct |
19 |
Correct |
1094 ms |
59168 KB |
Output is correct |
20 |
Execution timed out |
2083 ms |
97252 KB |
Time limit exceeded |