// 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, int lim = inf){
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--)
if(nxt[j][w] < lim)
ans = min(ans, 2 + solve(i, nxt[j][w], nxt[j][w+1]));
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 |
21 ms |
4992 KB |
Output is correct |
2 |
Correct |
23 ms |
5504 KB |
Output is correct |
3 |
Incorrect |
11 ms |
4352 KB |
Output isn't correct |
4 |
Correct |
24 ms |
5240 KB |
Output is correct |
5 |
Correct |
27 ms |
5504 KB |
Output is correct |
6 |
Correct |
47 ms |
6776 KB |
Output is correct |
7 |
Correct |
33 ms |
6392 KB |
Output is correct |
8 |
Correct |
2 ms |
3072 KB |
Output is correct |
9 |
Correct |
2 ms |
3072 KB |
Output is correct |
10 |
Correct |
2 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 |
20 ms |
4992 KB |
Output is correct |
14 |
Correct |
25 ms |
5496 KB |
Output is correct |
15 |
Incorrect |
12 ms |
4384 KB |
Output isn't correct |
16 |
Incorrect |
23 ms |
5496 KB |
Output isn't correct |
17 |
Incorrect |
23 ms |
5376 KB |
Output isn't correct |
18 |
Incorrect |
22 ms |
5120 KB |
Output isn't correct |
19 |
Correct |
22 ms |
4992 KB |
Output is correct |
20 |
Correct |
21 ms |
5112 KB |
Output is correct |
21 |
Correct |
22 ms |
5256 KB |
Output is correct |
22 |
Correct |
32 ms |
5488 KB |
Output is correct |
23 |
Correct |
16 ms |
4600 KB |
Output is correct |
24 |
Correct |
16 ms |
4736 KB |
Output is correct |
25 |
Incorrect |
14 ms |
4608 KB |
Output isn't correct |
26 |
Correct |
19 ms |
4992 KB |
Output is correct |
27 |
Correct |
32 ms |
5980 KB |
Output is correct |
28 |
Correct |
46 ms |
6776 KB |
Output is correct |
29 |
Correct |
32 ms |
6392 KB |
Output is correct |
30 |
Incorrect |
20 ms |
5240 KB |
Output isn't correct |
31 |
Correct |
18 ms |
4856 KB |
Output is correct |
32 |
Incorrect |
17 ms |
4992 KB |
Output isn't correct |
33 |
Incorrect |
25 ms |
5120 KB |
Output isn't correct |
34 |
Correct |
24 ms |
5632 KB |
Output is correct |
35 |
Correct |
29 ms |
6016 KB |
Output is correct |
36 |
Correct |
34 ms |
6392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2101 ms |
128296 KB |
Time limit exceeded |
2 |
Execution timed out |
2101 ms |
111608 KB |
Time limit exceeded |
3 |
Incorrect |
841 ms |
64972 KB |
Output isn't correct |
4 |
Execution timed out |
2102 ms |
145680 KB |
Time limit exceeded |
5 |
Execution timed out |
2094 ms |
102712 KB |
Time limit exceeded |
6 |
Execution timed out |
2097 ms |
107292 KB |
Time limit exceeded |
7 |
Execution timed out |
2107 ms |
142276 KB |
Time limit exceeded |
8 |
Execution timed out |
2095 ms |
150244 KB |
Time limit exceeded |
9 |
Execution timed out |
2096 ms |
110812 KB |
Time limit exceeded |
10 |
Execution timed out |
2086 ms |
107636 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2108 ms |
176804 KB |
Time limit exceeded |
2 |
Execution timed out |
2079 ms |
132660 KB |
Time limit exceeded |
3 |
Execution timed out |
2109 ms |
155896 KB |
Time limit exceeded |
4 |
Execution timed out |
2111 ms |
146680 KB |
Time limit exceeded |
5 |
Execution timed out |
2115 ms |
202876 KB |
Time limit exceeded |
6 |
Execution timed out |
2092 ms |
134476 KB |
Time limit exceeded |
7 |
Execution timed out |
2107 ms |
149080 KB |
Time limit exceeded |
8 |
Execution timed out |
2106 ms |
135408 KB |
Time limit exceeded |
9 |
Execution timed out |
2111 ms |
167160 KB |
Time limit exceeded |
10 |
Execution timed out |
2090 ms |
152444 KB |
Time limit exceeded |
11 |
Execution timed out |
2105 ms |
152864 KB |
Time limit exceeded |
12 |
Execution timed out |
2099 ms |
183832 KB |
Time limit exceeded |
13 |
Execution timed out |
2111 ms |
162680 KB |
Time limit exceeded |
14 |
Execution timed out |
2110 ms |
158968 KB |
Time limit exceeded |
15 |
Execution timed out |
2103 ms |
138360 KB |
Time limit exceeded |
16 |
Execution timed out |
2105 ms |
181420 KB |
Time limit exceeded |
17 |
Execution timed out |
2103 ms |
170164 KB |
Time limit exceeded |
18 |
Execution timed out |
2101 ms |
169244 KB |
Time limit exceeded |
19 |
Execution timed out |
2054 ms |
133624 KB |
Time limit exceeded |
20 |
Execution timed out |
2103 ms |
142820 KB |
Time limit exceeded |