#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<ll, ll>
#define f first
#define s second
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define MX(a,b) a=max(a,b)
#define MN(a,b) a = min(a,b)
#define FOR(i,a,b) for (int i = a; i<b; ++i)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
const int maxn = 1e5+5;
int ps[15][maxn];
int a[maxn];
int frm[15][1<<15];
int sf[15][maxn], sb[15][maxn]; // sig front, sig back
vector<int> where[15];
const int G = 15;
ll dp[1<<15];
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(1,2);
string _baa; cin>>_baa;
int n = SZ(_baa);
REP(i, n) {
a[i+1] = _baa[i] - 'A';
}
// a is 1-indexed!!
REP(g, G) {
REP1(i,n) {
ps[g][i] = ps[g][i-1] + (a[i] == g);
}
}
REP1(i,n) {
where[a[i]].pb(i);
}
REP(to,G) {
memset(sf, 0, sizeof sf); memset(sb, 0, sizeof sb);
REP(oth,G) {
REP1(i,n) {
sf[oth][i] = sf[oth][i-1];
if (a[i] == to) {
sf[oth][i] += ps[oth][i-1];
}
}
for (int i = n; i>=1; --i) {
sb[oth][i] = sb[oth][i+1];
if (a[i] == to) {
sb[oth][i] += (ps[oth][n] - ps[oth][i]);
}
}
}
REP(M, (1<<G)) {
if (M & (1<<to)) continue;
int bl = 0, br = SZ(where[to]); // first node where it's better to go to the right
while (bl != br) {
int bm = (bl + br) /2;
int i = where[to][bm];
int sl=ps[to][i-1],sr=(ps[to][n]-ps[to][i]);
REP(oth, G) {
if (M & (1<<oth)) {
sl += ps[oth][i-1] * 2;
sr += (ps[oth][n] - ps[oth][i]) * 2;
}
}
if (sr < sl) {
br = bm;
}else{
bl = bm+1;
}
}
if (bl-1 >=0) {
int i = where[to][bl-1];
frm[to][M] += sf[to][i];
REP(oth, G) {
if (M & (1<<oth)) {
frm[to][M] += sf[oth][i] * 2;
}
}
}
if (bl < SZ(where[to])) {
int i = where[to][bl];
frm[to][M] += sb[to][i];
REP(oth, G) {
if (M & (1<<oth)) {
frm[to][M] += sb[oth][i] * 2;
}
}
}
}
}
memset(dp, 0x3f, sizeof dp);
REP(M, (1<<G)) {
if (M == 0) {
dp[M] = 0; continue;
}
REP(j, G) {
if (M & (1<<j)) {
MN(dp[M], dp[M^(1<<j)] + frm[j][M^(1<<j)]);
}
}
}
ll re = dp[(1<<G)-1];
if (re % 2 == 0) cout<<re/2<<endl;
else cout<<(re/2)<<".5"<<endl;
// cout<<(dp[(1<<G)-1]/2.0)<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
24480 KB |
Output is correct |
2 |
Correct |
50 ms |
24392 KB |
Output is correct |
3 |
Correct |
51 ms |
24396 KB |
Output is correct |
4 |
Correct |
52 ms |
24396 KB |
Output is correct |
5 |
Correct |
55 ms |
24416 KB |
Output is correct |
6 |
Correct |
156 ms |
35052 KB |
Output is correct |
7 |
Correct |
173 ms |
37124 KB |
Output is correct |
8 |
Correct |
177 ms |
37868 KB |
Output is correct |
9 |
Correct |
173 ms |
37940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
24652 KB |
Output is correct |
2 |
Correct |
57 ms |
24292 KB |
Output is correct |
3 |
Correct |
89 ms |
25812 KB |
Output is correct |
4 |
Correct |
77 ms |
25932 KB |
Output is correct |
5 |
Correct |
65 ms |
25912 KB |
Output is correct |
6 |
Correct |
63 ms |
25860 KB |
Output is correct |
7 |
Correct |
103 ms |
25904 KB |
Output is correct |
8 |
Correct |
83 ms |
25676 KB |
Output is correct |
9 |
Correct |
75 ms |
25824 KB |
Output is correct |
10 |
Correct |
71 ms |
25860 KB |
Output is correct |
11 |
Correct |
71 ms |
25936 KB |
Output is correct |
12 |
Correct |
71 ms |
25808 KB |
Output is correct |
13 |
Correct |
73 ms |
25804 KB |
Output is correct |
14 |
Correct |
75 ms |
25944 KB |
Output is correct |
15 |
Correct |
72 ms |
26056 KB |
Output is correct |
16 |
Correct |
73 ms |
25936 KB |
Output is correct |
17 |
Correct |
73 ms |
25820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
24652 KB |
Output is correct |
2 |
Correct |
57 ms |
24292 KB |
Output is correct |
3 |
Correct |
89 ms |
25812 KB |
Output is correct |
4 |
Correct |
77 ms |
25932 KB |
Output is correct |
5 |
Correct |
65 ms |
25912 KB |
Output is correct |
6 |
Correct |
63 ms |
25860 KB |
Output is correct |
7 |
Correct |
103 ms |
25904 KB |
Output is correct |
8 |
Correct |
83 ms |
25676 KB |
Output is correct |
9 |
Correct |
75 ms |
25824 KB |
Output is correct |
10 |
Correct |
71 ms |
25860 KB |
Output is correct |
11 |
Correct |
71 ms |
25936 KB |
Output is correct |
12 |
Correct |
71 ms |
25808 KB |
Output is correct |
13 |
Correct |
73 ms |
25804 KB |
Output is correct |
14 |
Correct |
75 ms |
25944 KB |
Output is correct |
15 |
Correct |
72 ms |
26056 KB |
Output is correct |
16 |
Correct |
73 ms |
25936 KB |
Output is correct |
17 |
Correct |
73 ms |
25820 KB |
Output is correct |
18 |
Correct |
56 ms |
24780 KB |
Output is correct |
19 |
Correct |
55 ms |
24404 KB |
Output is correct |
20 |
Correct |
92 ms |
25872 KB |
Output is correct |
21 |
Correct |
73 ms |
25948 KB |
Output is correct |
22 |
Correct |
66 ms |
25932 KB |
Output is correct |
23 |
Correct |
65 ms |
25800 KB |
Output is correct |
24 |
Correct |
77 ms |
25940 KB |
Output is correct |
25 |
Correct |
71 ms |
25684 KB |
Output is correct |
26 |
Correct |
76 ms |
25936 KB |
Output is correct |
27 |
Correct |
75 ms |
25932 KB |
Output is correct |
28 |
Correct |
74 ms |
25856 KB |
Output is correct |
29 |
Correct |
74 ms |
25948 KB |
Output is correct |
30 |
Correct |
72 ms |
25816 KB |
Output is correct |
31 |
Correct |
74 ms |
25832 KB |
Output is correct |
32 |
Correct |
71 ms |
25932 KB |
Output is correct |
33 |
Correct |
73 ms |
25820 KB |
Output is correct |
34 |
Correct |
71 ms |
25828 KB |
Output is correct |
35 |
Correct |
68 ms |
25740 KB |
Output is correct |
36 |
Correct |
70 ms |
25872 KB |
Output is correct |
37 |
Correct |
135 ms |
28084 KB |
Output is correct |
38 |
Correct |
118 ms |
28044 KB |
Output is correct |
39 |
Correct |
84 ms |
28064 KB |
Output is correct |
40 |
Correct |
127 ms |
27940 KB |
Output is correct |
41 |
Correct |
138 ms |
27984 KB |
Output is correct |
42 |
Correct |
134 ms |
28012 KB |
Output is correct |
43 |
Correct |
131 ms |
28056 KB |
Output is correct |
44 |
Correct |
133 ms |
27944 KB |
Output is correct |
45 |
Correct |
131 ms |
27980 KB |
Output is correct |
46 |
Correct |
132 ms |
27988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
24480 KB |
Output is correct |
2 |
Correct |
50 ms |
24392 KB |
Output is correct |
3 |
Correct |
51 ms |
24396 KB |
Output is correct |
4 |
Correct |
52 ms |
24396 KB |
Output is correct |
5 |
Correct |
55 ms |
24416 KB |
Output is correct |
6 |
Correct |
156 ms |
35052 KB |
Output is correct |
7 |
Correct |
173 ms |
37124 KB |
Output is correct |
8 |
Correct |
177 ms |
37868 KB |
Output is correct |
9 |
Correct |
173 ms |
37940 KB |
Output is correct |
10 |
Correct |
56 ms |
24652 KB |
Output is correct |
11 |
Correct |
57 ms |
24292 KB |
Output is correct |
12 |
Correct |
89 ms |
25812 KB |
Output is correct |
13 |
Correct |
77 ms |
25932 KB |
Output is correct |
14 |
Correct |
65 ms |
25912 KB |
Output is correct |
15 |
Correct |
63 ms |
25860 KB |
Output is correct |
16 |
Correct |
103 ms |
25904 KB |
Output is correct |
17 |
Correct |
83 ms |
25676 KB |
Output is correct |
18 |
Correct |
75 ms |
25824 KB |
Output is correct |
19 |
Correct |
71 ms |
25860 KB |
Output is correct |
20 |
Correct |
71 ms |
25936 KB |
Output is correct |
21 |
Correct |
71 ms |
25808 KB |
Output is correct |
22 |
Correct |
73 ms |
25804 KB |
Output is correct |
23 |
Correct |
75 ms |
25944 KB |
Output is correct |
24 |
Correct |
72 ms |
26056 KB |
Output is correct |
25 |
Correct |
73 ms |
25936 KB |
Output is correct |
26 |
Correct |
73 ms |
25820 KB |
Output is correct |
27 |
Correct |
56 ms |
24780 KB |
Output is correct |
28 |
Correct |
55 ms |
24404 KB |
Output is correct |
29 |
Correct |
92 ms |
25872 KB |
Output is correct |
30 |
Correct |
73 ms |
25948 KB |
Output is correct |
31 |
Correct |
66 ms |
25932 KB |
Output is correct |
32 |
Correct |
65 ms |
25800 KB |
Output is correct |
33 |
Correct |
77 ms |
25940 KB |
Output is correct |
34 |
Correct |
71 ms |
25684 KB |
Output is correct |
35 |
Correct |
76 ms |
25936 KB |
Output is correct |
36 |
Correct |
75 ms |
25932 KB |
Output is correct |
37 |
Correct |
74 ms |
25856 KB |
Output is correct |
38 |
Correct |
74 ms |
25948 KB |
Output is correct |
39 |
Correct |
72 ms |
25816 KB |
Output is correct |
40 |
Correct |
74 ms |
25832 KB |
Output is correct |
41 |
Correct |
71 ms |
25932 KB |
Output is correct |
42 |
Correct |
73 ms |
25820 KB |
Output is correct |
43 |
Correct |
71 ms |
25828 KB |
Output is correct |
44 |
Correct |
68 ms |
25740 KB |
Output is correct |
45 |
Correct |
70 ms |
25872 KB |
Output is correct |
46 |
Correct |
135 ms |
28084 KB |
Output is correct |
47 |
Correct |
118 ms |
28044 KB |
Output is correct |
48 |
Correct |
84 ms |
28064 KB |
Output is correct |
49 |
Correct |
127 ms |
27940 KB |
Output is correct |
50 |
Correct |
138 ms |
27984 KB |
Output is correct |
51 |
Correct |
134 ms |
28012 KB |
Output is correct |
52 |
Correct |
131 ms |
28056 KB |
Output is correct |
53 |
Correct |
133 ms |
27944 KB |
Output is correct |
54 |
Correct |
131 ms |
27980 KB |
Output is correct |
55 |
Correct |
132 ms |
27988 KB |
Output is correct |
56 |
Correct |
63 ms |
25172 KB |
Output is correct |
57 |
Correct |
102 ms |
27348 KB |
Output is correct |
58 |
Correct |
57 ms |
24516 KB |
Output is correct |
59 |
Correct |
55 ms |
24280 KB |
Output is correct |
60 |
Correct |
53 ms |
24400 KB |
Output is correct |
61 |
Correct |
53 ms |
24276 KB |
Output is correct |
62 |
Correct |
59 ms |
24496 KB |
Output is correct |
63 |
Correct |
151 ms |
35056 KB |
Output is correct |
64 |
Correct |
166 ms |
37120 KB |
Output is correct |
65 |
Correct |
182 ms |
37848 KB |
Output is correct |
66 |
Correct |
182 ms |
37868 KB |
Output is correct |
67 |
Correct |
56 ms |
24652 KB |
Output is correct |
68 |
Correct |
61 ms |
24280 KB |
Output is correct |
69 |
Correct |
83 ms |
26028 KB |
Output is correct |
70 |
Correct |
73 ms |
25932 KB |
Output is correct |
71 |
Correct |
65 ms |
25940 KB |
Output is correct |
72 |
Correct |
63 ms |
25856 KB |
Output is correct |
73 |
Correct |
77 ms |
25884 KB |
Output is correct |
74 |
Correct |
82 ms |
25556 KB |
Output is correct |
75 |
Correct |
71 ms |
25812 KB |
Output is correct |
76 |
Correct |
78 ms |
25804 KB |
Output is correct |
77 |
Correct |
71 ms |
25808 KB |
Output is correct |
78 |
Correct |
70 ms |
25920 KB |
Output is correct |
79 |
Correct |
73 ms |
25840 KB |
Output is correct |
80 |
Correct |
76 ms |
25928 KB |
Output is correct |
81 |
Correct |
70 ms |
25932 KB |
Output is correct |
82 |
Correct |
76 ms |
25940 KB |
Output is correct |
83 |
Correct |
72 ms |
25880 KB |
Output is correct |
84 |
Correct |
73 ms |
25804 KB |
Output is correct |
85 |
Correct |
68 ms |
25804 KB |
Output is correct |
86 |
Correct |
144 ms |
28104 KB |
Output is correct |
87 |
Correct |
116 ms |
27920 KB |
Output is correct |
88 |
Correct |
84 ms |
28044 KB |
Output is correct |
89 |
Correct |
130 ms |
28136 KB |
Output is correct |
90 |
Correct |
130 ms |
28012 KB |
Output is correct |
91 |
Correct |
146 ms |
28016 KB |
Output is correct |
92 |
Correct |
141 ms |
27956 KB |
Output is correct |
93 |
Correct |
143 ms |
28020 KB |
Output is correct |
94 |
Correct |
143 ms |
27980 KB |
Output is correct |
95 |
Correct |
136 ms |
28004 KB |
Output is correct |
96 |
Correct |
310 ms |
40952 KB |
Output is correct |
97 |
Correct |
91 ms |
27228 KB |
Output is correct |
98 |
Correct |
264 ms |
40968 KB |
Output is correct |
99 |
Correct |
198 ms |
40980 KB |
Output is correct |
100 |
Correct |
80 ms |
27340 KB |
Output is correct |
101 |
Correct |
278 ms |
40976 KB |
Output is correct |
102 |
Correct |
300 ms |
41116 KB |
Output is correct |
103 |
Correct |
298 ms |
41124 KB |
Output is correct |
104 |
Correct |
298 ms |
41164 KB |
Output is correct |
105 |
Correct |
303 ms |
41064 KB |
Output is correct |
106 |
Correct |
302 ms |
41172 KB |
Output is correct |
107 |
Correct |
301 ms |
41076 KB |
Output is correct |
108 |
Correct |
286 ms |
40908 KB |
Output is correct |
109 |
Correct |
299 ms |
41268 KB |
Output is correct |