#include <iostream>
#include <algorithm>
#include <functional>
#include <random>
#include <cmath>
#include <vector>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <cassert>
#include <string>
#include <bitset>
#include <numeric>
#include <iomanip>
#include <limits.h>
#include <tuple>
// #pragma GCC optimization ("unroll-loops")
// #pragma GCC optimization ("O3")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = ll(1e9) + 7, maxn = 1e5 + 5, maxg = 15, lg = 22, inf = ll(1e18) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
int a[maxn], lst[maxg], cnt[maxg];
vector<int> pos[maxg];
ll cnt_prf[maxn][maxg], cnt_suf[maxn][maxg];
ll dp[1<<maxg];
int main(){
cin.tie(0) -> sync_with_stdio(0);
string s; cin >> s;
int n = sz(s);
rep(i,0,n) a[i] = s[i]-'A', pos[a[i]].pb(i);
rep(i,0,maxg) lst[i] = -1;
rep(i,0,n){
rep(j,0,maxg){
cnt_prf[i][j] = (lst[a[i]] == -1? 0: cnt_prf[lst[a[i]]][j]) + cnt[j];
}
lst[a[i]] = i, cnt[a[i]]++;
}
rep(i,0,maxg) lst[i] = n, cnt[i] = 0;
per(i,n-1,0){
rep(j,0,maxg){
cnt_suf[i][j] = (lst[a[i]] == n? 0: cnt_suf[lst[a[i]]][j]) + cnt[j];
}
lst[a[i]] = i, cnt[a[i]]++;
}
rep(i,0,n) rep(j,0,maxg) cnt_prf[i][j] <<= 1ll, cnt_suf[i][j] <<= 1ll;
rep(msk,1,1<<maxg){
vector<int> bt;
rep(j,0,maxg) if(msk>>j&1) bt.pb(j);
dp[msk] = inf;
for(int c: bt){
if(cnt[c] == 0){
dp[msk] = dp[msk^(1<<c)];
break;
}
auto calc = [&](ll x){
ll res = x*(x-1)/2ll + (cnt[c]-x)*(cnt[c]-x-1)/2ll;
for(int r: bt) if(r - c){
if(x > 0 && x <= cnt[c]) res += cnt_prf[pos[c][x-1]][r];
if(x < cnt[c] && x >= 0) res += cnt_suf[pos[c][x]][r];
}
return res;
};
// rep(i,0,cnt[c]+1){
// dp[msk] = min(dp[msk], dp[msk^(1<<c)] + calc(i));
// }
int lx = 0, rx = cnt[c];
while(lx + 1 < rx){
int mid = (lx + rx)>>1;
ll l = calc(mid), r = calc(mid+1);
if(l >= r) lx = mid;
else rx = mid;
}
dp[msk] = min(dp[msk], dp[msk^(1<<c)] + min(calc(lx), calc(rx)));
}
}
cout << dp[(1<<maxg)-1]/2ll;
if(dp[(1<<maxg)-1]&1ll) cout << ".5";
cout << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
724 KB |
Output is correct |
2 |
Correct |
7 ms |
484 KB |
Output is correct |
3 |
Correct |
10 ms |
528 KB |
Output is correct |
4 |
Correct |
9 ms |
496 KB |
Output is correct |
5 |
Correct |
13 ms |
724 KB |
Output is correct |
6 |
Correct |
43 ms |
19896 KB |
Output is correct |
7 |
Correct |
39 ms |
23688 KB |
Output is correct |
8 |
Correct |
32 ms |
25184 KB |
Output is correct |
9 |
Correct |
34 ms |
25180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
520 KB |
Output is correct |
2 |
Correct |
8 ms |
596 KB |
Output is correct |
3 |
Correct |
32 ms |
544 KB |
Output is correct |
4 |
Correct |
39 ms |
596 KB |
Output is correct |
5 |
Correct |
16 ms |
528 KB |
Output is correct |
6 |
Correct |
13 ms |
552 KB |
Output is correct |
7 |
Correct |
31 ms |
524 KB |
Output is correct |
8 |
Correct |
14 ms |
596 KB |
Output is correct |
9 |
Correct |
22 ms |
484 KB |
Output is correct |
10 |
Correct |
22 ms |
588 KB |
Output is correct |
11 |
Correct |
20 ms |
584 KB |
Output is correct |
12 |
Correct |
22 ms |
528 KB |
Output is correct |
13 |
Correct |
22 ms |
568 KB |
Output is correct |
14 |
Correct |
22 ms |
520 KB |
Output is correct |
15 |
Correct |
21 ms |
532 KB |
Output is correct |
16 |
Correct |
21 ms |
588 KB |
Output is correct |
17 |
Correct |
23 ms |
572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
520 KB |
Output is correct |
2 |
Correct |
8 ms |
596 KB |
Output is correct |
3 |
Correct |
32 ms |
544 KB |
Output is correct |
4 |
Correct |
39 ms |
596 KB |
Output is correct |
5 |
Correct |
16 ms |
528 KB |
Output is correct |
6 |
Correct |
13 ms |
552 KB |
Output is correct |
7 |
Correct |
31 ms |
524 KB |
Output is correct |
8 |
Correct |
14 ms |
596 KB |
Output is correct |
9 |
Correct |
22 ms |
484 KB |
Output is correct |
10 |
Correct |
22 ms |
588 KB |
Output is correct |
11 |
Correct |
20 ms |
584 KB |
Output is correct |
12 |
Correct |
22 ms |
528 KB |
Output is correct |
13 |
Correct |
22 ms |
568 KB |
Output is correct |
14 |
Correct |
22 ms |
520 KB |
Output is correct |
15 |
Correct |
21 ms |
532 KB |
Output is correct |
16 |
Correct |
21 ms |
588 KB |
Output is correct |
17 |
Correct |
23 ms |
572 KB |
Output is correct |
18 |
Correct |
9 ms |
596 KB |
Output is correct |
19 |
Correct |
7 ms |
596 KB |
Output is correct |
20 |
Correct |
31 ms |
496 KB |
Output is correct |
21 |
Correct |
29 ms |
596 KB |
Output is correct |
22 |
Correct |
15 ms |
596 KB |
Output is correct |
23 |
Correct |
12 ms |
596 KB |
Output is correct |
24 |
Correct |
30 ms |
584 KB |
Output is correct |
25 |
Correct |
12 ms |
544 KB |
Output is correct |
26 |
Correct |
22 ms |
596 KB |
Output is correct |
27 |
Correct |
28 ms |
572 KB |
Output is correct |
28 |
Correct |
21 ms |
468 KB |
Output is correct |
29 |
Correct |
21 ms |
608 KB |
Output is correct |
30 |
Correct |
23 ms |
572 KB |
Output is correct |
31 |
Correct |
23 ms |
572 KB |
Output is correct |
32 |
Correct |
21 ms |
504 KB |
Output is correct |
33 |
Correct |
21 ms |
568 KB |
Output is correct |
34 |
Correct |
24 ms |
516 KB |
Output is correct |
35 |
Correct |
8 ms |
3028 KB |
Output is correct |
36 |
Correct |
7 ms |
3028 KB |
Output is correct |
37 |
Correct |
95 ms |
3020 KB |
Output is correct |
38 |
Correct |
79 ms |
2988 KB |
Output is correct |
39 |
Correct |
22 ms |
3024 KB |
Output is correct |
40 |
Correct |
84 ms |
2984 KB |
Output is correct |
41 |
Correct |
82 ms |
3012 KB |
Output is correct |
42 |
Correct |
89 ms |
2948 KB |
Output is correct |
43 |
Correct |
86 ms |
2904 KB |
Output is correct |
44 |
Correct |
86 ms |
2984 KB |
Output is correct |
45 |
Correct |
85 ms |
2980 KB |
Output is correct |
46 |
Correct |
85 ms |
2916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
724 KB |
Output is correct |
2 |
Correct |
7 ms |
484 KB |
Output is correct |
3 |
Correct |
10 ms |
528 KB |
Output is correct |
4 |
Correct |
9 ms |
496 KB |
Output is correct |
5 |
Correct |
13 ms |
724 KB |
Output is correct |
6 |
Correct |
43 ms |
19896 KB |
Output is correct |
7 |
Correct |
39 ms |
23688 KB |
Output is correct |
8 |
Correct |
32 ms |
25184 KB |
Output is correct |
9 |
Correct |
34 ms |
25180 KB |
Output is correct |
10 |
Correct |
9 ms |
520 KB |
Output is correct |
11 |
Correct |
8 ms |
596 KB |
Output is correct |
12 |
Correct |
32 ms |
544 KB |
Output is correct |
13 |
Correct |
39 ms |
596 KB |
Output is correct |
14 |
Correct |
16 ms |
528 KB |
Output is correct |
15 |
Correct |
13 ms |
552 KB |
Output is correct |
16 |
Correct |
31 ms |
524 KB |
Output is correct |
17 |
Correct |
14 ms |
596 KB |
Output is correct |
18 |
Correct |
22 ms |
484 KB |
Output is correct |
19 |
Correct |
22 ms |
588 KB |
Output is correct |
20 |
Correct |
20 ms |
584 KB |
Output is correct |
21 |
Correct |
22 ms |
528 KB |
Output is correct |
22 |
Correct |
22 ms |
568 KB |
Output is correct |
23 |
Correct |
22 ms |
520 KB |
Output is correct |
24 |
Correct |
21 ms |
532 KB |
Output is correct |
25 |
Correct |
21 ms |
588 KB |
Output is correct |
26 |
Correct |
23 ms |
572 KB |
Output is correct |
27 |
Correct |
9 ms |
596 KB |
Output is correct |
28 |
Correct |
7 ms |
596 KB |
Output is correct |
29 |
Correct |
31 ms |
496 KB |
Output is correct |
30 |
Correct |
29 ms |
596 KB |
Output is correct |
31 |
Correct |
15 ms |
596 KB |
Output is correct |
32 |
Correct |
12 ms |
596 KB |
Output is correct |
33 |
Correct |
30 ms |
584 KB |
Output is correct |
34 |
Correct |
12 ms |
544 KB |
Output is correct |
35 |
Correct |
22 ms |
596 KB |
Output is correct |
36 |
Correct |
28 ms |
572 KB |
Output is correct |
37 |
Correct |
21 ms |
468 KB |
Output is correct |
38 |
Correct |
21 ms |
608 KB |
Output is correct |
39 |
Correct |
23 ms |
572 KB |
Output is correct |
40 |
Correct |
23 ms |
572 KB |
Output is correct |
41 |
Correct |
21 ms |
504 KB |
Output is correct |
42 |
Correct |
21 ms |
568 KB |
Output is correct |
43 |
Correct |
24 ms |
516 KB |
Output is correct |
44 |
Correct |
8 ms |
3028 KB |
Output is correct |
45 |
Correct |
7 ms |
3028 KB |
Output is correct |
46 |
Correct |
95 ms |
3020 KB |
Output is correct |
47 |
Correct |
79 ms |
2988 KB |
Output is correct |
48 |
Correct |
22 ms |
3024 KB |
Output is correct |
49 |
Correct |
84 ms |
2984 KB |
Output is correct |
50 |
Correct |
82 ms |
3012 KB |
Output is correct |
51 |
Correct |
89 ms |
2948 KB |
Output is correct |
52 |
Correct |
86 ms |
2904 KB |
Output is correct |
53 |
Correct |
86 ms |
2984 KB |
Output is correct |
54 |
Correct |
85 ms |
2980 KB |
Output is correct |
55 |
Correct |
85 ms |
2916 KB |
Output is correct |
56 |
Correct |
6 ms |
596 KB |
Output is correct |
57 |
Correct |
24 ms |
652 KB |
Output is correct |
58 |
Correct |
15 ms |
720 KB |
Output is correct |
59 |
Correct |
10 ms |
596 KB |
Output is correct |
60 |
Correct |
10 ms |
532 KB |
Output is correct |
61 |
Correct |
9 ms |
548 KB |
Output is correct |
62 |
Correct |
13 ms |
724 KB |
Output is correct |
63 |
Correct |
29 ms |
19976 KB |
Output is correct |
64 |
Correct |
30 ms |
23688 KB |
Output is correct |
65 |
Correct |
32 ms |
25256 KB |
Output is correct |
66 |
Correct |
35 ms |
25236 KB |
Output is correct |
67 |
Correct |
9 ms |
596 KB |
Output is correct |
68 |
Correct |
8 ms |
624 KB |
Output is correct |
69 |
Correct |
37 ms |
632 KB |
Output is correct |
70 |
Correct |
34 ms |
548 KB |
Output is correct |
71 |
Correct |
16 ms |
544 KB |
Output is correct |
72 |
Correct |
15 ms |
540 KB |
Output is correct |
73 |
Correct |
40 ms |
544 KB |
Output is correct |
74 |
Correct |
13 ms |
564 KB |
Output is correct |
75 |
Correct |
21 ms |
560 KB |
Output is correct |
76 |
Correct |
20 ms |
596 KB |
Output is correct |
77 |
Correct |
21 ms |
500 KB |
Output is correct |
78 |
Correct |
21 ms |
580 KB |
Output is correct |
79 |
Correct |
21 ms |
592 KB |
Output is correct |
80 |
Correct |
26 ms |
584 KB |
Output is correct |
81 |
Correct |
21 ms |
484 KB |
Output is correct |
82 |
Correct |
22 ms |
596 KB |
Output is correct |
83 |
Correct |
21 ms |
588 KB |
Output is correct |
84 |
Correct |
8 ms |
3028 KB |
Output is correct |
85 |
Correct |
8 ms |
3020 KB |
Output is correct |
86 |
Correct |
89 ms |
3072 KB |
Output is correct |
87 |
Correct |
111 ms |
2952 KB |
Output is correct |
88 |
Correct |
22 ms |
3020 KB |
Output is correct |
89 |
Correct |
81 ms |
3016 KB |
Output is correct |
90 |
Correct |
92 ms |
3088 KB |
Output is correct |
91 |
Correct |
85 ms |
2960 KB |
Output is correct |
92 |
Correct |
90 ms |
2972 KB |
Output is correct |
93 |
Correct |
124 ms |
2984 KB |
Output is correct |
94 |
Correct |
84 ms |
3008 KB |
Output is correct |
95 |
Correct |
93 ms |
3116 KB |
Output is correct |
96 |
Correct |
187 ms |
25204 KB |
Output is correct |
97 |
Correct |
35 ms |
596 KB |
Output is correct |
98 |
Correct |
163 ms |
25012 KB |
Output is correct |
99 |
Correct |
42 ms |
25228 KB |
Output is correct |
100 |
Correct |
17 ms |
528 KB |
Output is correct |
101 |
Correct |
169 ms |
25068 KB |
Output is correct |
102 |
Correct |
174 ms |
25224 KB |
Output is correct |
103 |
Correct |
166 ms |
25020 KB |
Output is correct |
104 |
Correct |
179 ms |
25132 KB |
Output is correct |
105 |
Correct |
169 ms |
25164 KB |
Output is correct |
106 |
Correct |
170 ms |
25060 KB |
Output is correct |
107 |
Correct |
177 ms |
25212 KB |
Output is correct |
108 |
Correct |
171 ms |
25020 KB |
Output is correct |
109 |
Correct |
178 ms |
25144 KB |
Output is correct |