#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=7e4+7, INF=1e9+7;
vector<pair<int,int>>V[LIM];
int nxt[LIM][10], odl[LIM][10], odl2[LIM], kiedy[10];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, ans=INF, x=0;
string s;
cin >> n >> s;
rep(i, n) {
if(s[i]=='e') {
s[i]='a';
x=i;
} else if(s[i]=='a') s[i]='e';
}
rep(i, 10) kiedy[i]=INF;
for(int i=n-1; i>=0; --i) {
rep(j, 10) {
nxt[i][j]=kiedy[j];
odl[i][j]=INF;
if(j && kiedy[j]<INF) V[kiedy[j]].pb({i, 2});
}
odl2[i]=INF;
kiedy[s[i]-'a']=i;
}
rep(i, n-1) V[i].pb({i+1, 1});
priority_queue<pair<int,pair<int,int>>>q;
q.push({0, {x, 0}});
while(!q.empty()) {
int o=-q.top().st, p=q.top().nd.st; q.pop();
if(odl2[p]<=o) continue;
odl2[p]=o;
for(auto i : V[p]) if(odl2[i.st]>o+i.nd) {
q.push({-o-i.nd, {i.st, 0}});
}
}
q.push({0, {0, 0}});
while(!q.empty()) {
int o=-q.top().st, a=q.top().nd.st, b=q.top().nd.nd; q.pop();
if(o>=odl[a][b]) continue;
odl[a][b]=o;
int p=nxt[a][b];
if(!b) p=a;
if(nxt[p][0]==INF) {
ans=min(ans, o);
} else {
ans=min(ans, o+odl2[a]+x-nxt[p][0]);
}
for(int i=1; i<10; ++i) if(nxt[a][i]<INF && nxt[a][i]>=p) {
if(nxt[p][0]>nxt[a][i]) {
if(odl[nxt[a][i]][i]==INF) q.push({-o-2, {nxt[a][i], 0}});
} else {
int mi=INF, c=i;
for(int j=1; j<10; ++j) mi=min(mi, nxt[nxt[p][0]][j]);
if(nxt[a][i]==mi) c=0;
if(odl[mi][c]==INF) q.push({-o-2-nxt[a][i]+nxt[p][0], {mi, c}});
}
}
}
rep(i, n) if(s[i]=='a') ++ans;
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2124 KB |
Output is correct |
2 |
Correct |
2 ms |
2080 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2068 KB |
Output isn't correct |
4 |
Correct |
3 ms |
1996 KB |
Output is correct |
5 |
Correct |
3 ms |
2096 KB |
Output is correct |
6 |
Correct |
3 ms |
1996 KB |
Output is correct |
7 |
Correct |
3 ms |
1996 KB |
Output is correct |
8 |
Correct |
1 ms |
1968 KB |
Output is correct |
9 |
Correct |
1 ms |
1868 KB |
Output is correct |
10 |
Correct |
2 ms |
1964 KB |
Output is correct |
11 |
Correct |
1 ms |
1868 KB |
Output is correct |
12 |
Correct |
1 ms |
1868 KB |
Output is correct |
13 |
Correct |
3 ms |
2124 KB |
Output is correct |
14 |
Correct |
2 ms |
1996 KB |
Output is correct |
15 |
Incorrect |
3 ms |
1996 KB |
Output isn't correct |
16 |
Incorrect |
3 ms |
1996 KB |
Output isn't correct |
17 |
Correct |
3 ms |
1996 KB |
Output is correct |
18 |
Correct |
2 ms |
1996 KB |
Output is correct |
19 |
Correct |
2 ms |
1996 KB |
Output is correct |
20 |
Correct |
3 ms |
1996 KB |
Output is correct |
21 |
Correct |
3 ms |
2104 KB |
Output is correct |
22 |
Correct |
3 ms |
2124 KB |
Output is correct |
23 |
Correct |
2 ms |
1996 KB |
Output is correct |
24 |
Correct |
3 ms |
1996 KB |
Output is correct |
25 |
Correct |
2 ms |
1996 KB |
Output is correct |
26 |
Correct |
2 ms |
2016 KB |
Output is correct |
27 |
Correct |
3 ms |
2016 KB |
Output is correct |
28 |
Correct |
3 ms |
2008 KB |
Output is correct |
29 |
Correct |
3 ms |
1996 KB |
Output is correct |
30 |
Incorrect |
3 ms |
1996 KB |
Output isn't correct |
31 |
Correct |
3 ms |
2124 KB |
Output is correct |
32 |
Incorrect |
2 ms |
1996 KB |
Output isn't correct |
33 |
Correct |
2 ms |
1996 KB |
Output is correct |
34 |
Correct |
3 ms |
1996 KB |
Output is correct |
35 |
Correct |
3 ms |
1996 KB |
Output is correct |
36 |
Correct |
3 ms |
1996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
3112 KB |
Output is correct |
2 |
Correct |
26 ms |
3020 KB |
Output is correct |
3 |
Correct |
11 ms |
2636 KB |
Output is correct |
4 |
Correct |
21 ms |
3116 KB |
Output is correct |
5 |
Incorrect |
23 ms |
3208 KB |
Output isn't correct |
6 |
Incorrect |
19 ms |
3472 KB |
Output isn't correct |
7 |
Incorrect |
18 ms |
3276 KB |
Output isn't correct |
8 |
Incorrect |
19 ms |
3108 KB |
Output isn't correct |
9 |
Correct |
23 ms |
3072 KB |
Output is correct |
10 |
Incorrect |
23 ms |
3024 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
242 ms |
17340 KB |
Output isn't correct |
2 |
Incorrect |
140 ms |
13004 KB |
Output isn't correct |
3 |
Incorrect |
178 ms |
14020 KB |
Output isn't correct |
4 |
Incorrect |
195 ms |
17980 KB |
Output isn't correct |
5 |
Incorrect |
278 ms |
19308 KB |
Output isn't correct |
6 |
Incorrect |
211 ms |
15180 KB |
Output isn't correct |
7 |
Incorrect |
315 ms |
18360 KB |
Output isn't correct |
8 |
Incorrect |
255 ms |
18176 KB |
Output isn't correct |
9 |
Incorrect |
271 ms |
18352 KB |
Output isn't correct |
10 |
Incorrect |
242 ms |
15972 KB |
Output isn't correct |
11 |
Incorrect |
195 ms |
17972 KB |
Output isn't correct |
12 |
Incorrect |
273 ms |
19364 KB |
Output isn't correct |
13 |
Incorrect |
206 ms |
15592 KB |
Output isn't correct |
14 |
Incorrect |
183 ms |
14692 KB |
Output isn't correct |
15 |
Incorrect |
280 ms |
16616 KB |
Output isn't correct |
16 |
Incorrect |
172 ms |
15556 KB |
Output isn't correct |
17 |
Incorrect |
238 ms |
17452 KB |
Output isn't correct |
18 |
Incorrect |
182 ms |
14064 KB |
Output isn't correct |
19 |
Incorrect |
150 ms |
13004 KB |
Output isn't correct |
20 |
Incorrect |
247 ms |
14792 KB |
Output isn't correct |