#include <bits/stdc++.h>
#define iter(a) a.begin(), a.end()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(iter(a), greater<>())
#define eb emplace_back
#define ef emplace_front
#define pob pop_back()
#define pof pop_front()
#define mp make_pair
#define F first
#define S second
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) { \
for(auto pv : a) b << pv << " "; \
b << "\n"; \
}
using namespace std;
typedef long long ll;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
int n = s.size();
const int G = 15;
vector<vector<int>> pos(G, vector<int>(1));
vector<int> id(n);
for(int i = 0; i < n; i++){
id[i] = pos[s[i] - 'A'].size();
pos[s[i] - 'A'].eb(i);
}
vector<vector<vector<ll>>> f(G, vector<vector<ll>>(G));
for(int a = 0; a < G; a++){
for(int b = 0; b < G; b++){
f[a][b].resize(pos[a].size() + 1);
ll total = 0;
ll cnt = 0;
for(int i = 0; i < n; i++){
if(s[i] == 'A' + a){
total += cnt;
f[a][b][id[i]] += total;
}
if(s[i] == 'A' + b) cnt++;
}
total = 0;
cnt = 0;
for(int i = n - 1; i >= 0; i--){
if(s[i] == 'A' + a){
f[a][b][id[i]] += total;
total += cnt;
}
if(s[i] == 'A' + b) cnt++;
}
f[a][b][0] = total;
}
}
auto fmsk = [&](int a, int msk, int i){
ll ans = 0;
for(int j = 0; j < G; j++){
if(1 << j & msk) ans += f[a][j][i];
}
return 2 * ans + f[a][a][i];
};
auto best = [&](int a, int msk){
int l = 0, r = pos[a].size(); r--;
while(l < r){
int mid = (l + r) / 2;
if(fmsk(a, msk, mid) >= fmsk(a, msk, mid + 1)) l = mid + 1;
else r = mid;
}
//cerr << "best " << a << " " << bitset<G>(msk) << " : " << l << " " << fmsk(a, msk, l) << "\n";
return l;
};
vector<ll> dp(1 << G, 1LL << 60);
dp[0] = 0;
for(int msk = 0; msk < (1 << G); msk++){
//cerr << "dp " << bitset<G>(msk) << " : " << dp[msk] << "\n";
for(int a = 0; a < G; a++){
if(1 << a & msk) continue;
int nxt = msk | (1 << a);
int b = best(a, msk);
ll t = dp[msk] + fmsk(a, msk, b);
dp[nxt] = min(dp[nxt], t);
//cerr << "trans " << bitset<G>(msk) << " -> " << bitset<G>(nxt) << " " << t << "\n";
}
}
ll ans = dp[(1 << G) - 1];
if(ans % 2 == 0) cout << ans / 2 << "\n";
else cout << ans / 2 << ".5\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
596 KB |
Output is correct |
2 |
Correct |
15 ms |
596 KB |
Output is correct |
3 |
Correct |
17 ms |
596 KB |
Output is correct |
4 |
Correct |
16 ms |
596 KB |
Output is correct |
5 |
Correct |
24 ms |
724 KB |
Output is correct |
6 |
Correct |
68 ms |
10524 KB |
Output is correct |
7 |
Correct |
77 ms |
12584 KB |
Output is correct |
8 |
Correct |
77 ms |
13244 KB |
Output is correct |
9 |
Correct |
83 ms |
13352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
596 KB |
Output is correct |
2 |
Correct |
21 ms |
596 KB |
Output is correct |
3 |
Correct |
44 ms |
596 KB |
Output is correct |
4 |
Correct |
39 ms |
596 KB |
Output is correct |
5 |
Correct |
30 ms |
596 KB |
Output is correct |
6 |
Correct |
26 ms |
596 KB |
Output is correct |
7 |
Correct |
36 ms |
596 KB |
Output is correct |
8 |
Correct |
36 ms |
596 KB |
Output is correct |
9 |
Correct |
35 ms |
596 KB |
Output is correct |
10 |
Correct |
31 ms |
596 KB |
Output is correct |
11 |
Correct |
32 ms |
596 KB |
Output is correct |
12 |
Correct |
33 ms |
596 KB |
Output is correct |
13 |
Correct |
33 ms |
596 KB |
Output is correct |
14 |
Correct |
32 ms |
596 KB |
Output is correct |
15 |
Correct |
32 ms |
596 KB |
Output is correct |
16 |
Correct |
34 ms |
596 KB |
Output is correct |
17 |
Correct |
33 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
596 KB |
Output is correct |
2 |
Correct |
21 ms |
596 KB |
Output is correct |
3 |
Correct |
44 ms |
596 KB |
Output is correct |
4 |
Correct |
39 ms |
596 KB |
Output is correct |
5 |
Correct |
30 ms |
596 KB |
Output is correct |
6 |
Correct |
26 ms |
596 KB |
Output is correct |
7 |
Correct |
36 ms |
596 KB |
Output is correct |
8 |
Correct |
36 ms |
596 KB |
Output is correct |
9 |
Correct |
35 ms |
596 KB |
Output is correct |
10 |
Correct |
31 ms |
596 KB |
Output is correct |
11 |
Correct |
32 ms |
596 KB |
Output is correct |
12 |
Correct |
33 ms |
596 KB |
Output is correct |
13 |
Correct |
33 ms |
596 KB |
Output is correct |
14 |
Correct |
32 ms |
596 KB |
Output is correct |
15 |
Correct |
32 ms |
596 KB |
Output is correct |
16 |
Correct |
34 ms |
596 KB |
Output is correct |
17 |
Correct |
33 ms |
596 KB |
Output is correct |
18 |
Correct |
22 ms |
596 KB |
Output is correct |
19 |
Correct |
21 ms |
596 KB |
Output is correct |
20 |
Correct |
41 ms |
596 KB |
Output is correct |
21 |
Correct |
44 ms |
596 KB |
Output is correct |
22 |
Correct |
28 ms |
596 KB |
Output is correct |
23 |
Correct |
26 ms |
596 KB |
Output is correct |
24 |
Correct |
36 ms |
596 KB |
Output is correct |
25 |
Correct |
33 ms |
596 KB |
Output is correct |
26 |
Correct |
34 ms |
596 KB |
Output is correct |
27 |
Correct |
31 ms |
596 KB |
Output is correct |
28 |
Correct |
30 ms |
596 KB |
Output is correct |
29 |
Correct |
32 ms |
596 KB |
Output is correct |
30 |
Correct |
32 ms |
596 KB |
Output is correct |
31 |
Correct |
33 ms |
468 KB |
Output is correct |
32 |
Correct |
32 ms |
596 KB |
Output is correct |
33 |
Correct |
32 ms |
596 KB |
Output is correct |
34 |
Correct |
33 ms |
596 KB |
Output is correct |
35 |
Correct |
31 ms |
1876 KB |
Output is correct |
36 |
Correct |
31 ms |
1876 KB |
Output is correct |
37 |
Correct |
83 ms |
1800 KB |
Output is correct |
38 |
Correct |
80 ms |
1748 KB |
Output is correct |
39 |
Correct |
40 ms |
1876 KB |
Output is correct |
40 |
Correct |
79 ms |
1876 KB |
Output is correct |
41 |
Correct |
84 ms |
1876 KB |
Output is correct |
42 |
Correct |
84 ms |
1776 KB |
Output is correct |
43 |
Correct |
80 ms |
1876 KB |
Output is correct |
44 |
Correct |
83 ms |
1748 KB |
Output is correct |
45 |
Correct |
84 ms |
1876 KB |
Output is correct |
46 |
Correct |
81 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
596 KB |
Output is correct |
2 |
Correct |
15 ms |
596 KB |
Output is correct |
3 |
Correct |
17 ms |
596 KB |
Output is correct |
4 |
Correct |
16 ms |
596 KB |
Output is correct |
5 |
Correct |
24 ms |
724 KB |
Output is correct |
6 |
Correct |
68 ms |
10524 KB |
Output is correct |
7 |
Correct |
77 ms |
12584 KB |
Output is correct |
8 |
Correct |
77 ms |
13244 KB |
Output is correct |
9 |
Correct |
83 ms |
13352 KB |
Output is correct |
10 |
Correct |
22 ms |
596 KB |
Output is correct |
11 |
Correct |
21 ms |
596 KB |
Output is correct |
12 |
Correct |
44 ms |
596 KB |
Output is correct |
13 |
Correct |
39 ms |
596 KB |
Output is correct |
14 |
Correct |
30 ms |
596 KB |
Output is correct |
15 |
Correct |
26 ms |
596 KB |
Output is correct |
16 |
Correct |
36 ms |
596 KB |
Output is correct |
17 |
Correct |
36 ms |
596 KB |
Output is correct |
18 |
Correct |
35 ms |
596 KB |
Output is correct |
19 |
Correct |
31 ms |
596 KB |
Output is correct |
20 |
Correct |
32 ms |
596 KB |
Output is correct |
21 |
Correct |
33 ms |
596 KB |
Output is correct |
22 |
Correct |
33 ms |
596 KB |
Output is correct |
23 |
Correct |
32 ms |
596 KB |
Output is correct |
24 |
Correct |
32 ms |
596 KB |
Output is correct |
25 |
Correct |
34 ms |
596 KB |
Output is correct |
26 |
Correct |
33 ms |
596 KB |
Output is correct |
27 |
Correct |
22 ms |
596 KB |
Output is correct |
28 |
Correct |
21 ms |
596 KB |
Output is correct |
29 |
Correct |
41 ms |
596 KB |
Output is correct |
30 |
Correct |
44 ms |
596 KB |
Output is correct |
31 |
Correct |
28 ms |
596 KB |
Output is correct |
32 |
Correct |
26 ms |
596 KB |
Output is correct |
33 |
Correct |
36 ms |
596 KB |
Output is correct |
34 |
Correct |
33 ms |
596 KB |
Output is correct |
35 |
Correct |
34 ms |
596 KB |
Output is correct |
36 |
Correct |
31 ms |
596 KB |
Output is correct |
37 |
Correct |
30 ms |
596 KB |
Output is correct |
38 |
Correct |
32 ms |
596 KB |
Output is correct |
39 |
Correct |
32 ms |
596 KB |
Output is correct |
40 |
Correct |
33 ms |
468 KB |
Output is correct |
41 |
Correct |
32 ms |
596 KB |
Output is correct |
42 |
Correct |
32 ms |
596 KB |
Output is correct |
43 |
Correct |
33 ms |
596 KB |
Output is correct |
44 |
Correct |
31 ms |
1876 KB |
Output is correct |
45 |
Correct |
31 ms |
1876 KB |
Output is correct |
46 |
Correct |
83 ms |
1800 KB |
Output is correct |
47 |
Correct |
80 ms |
1748 KB |
Output is correct |
48 |
Correct |
40 ms |
1876 KB |
Output is correct |
49 |
Correct |
79 ms |
1876 KB |
Output is correct |
50 |
Correct |
84 ms |
1876 KB |
Output is correct |
51 |
Correct |
84 ms |
1776 KB |
Output is correct |
52 |
Correct |
80 ms |
1876 KB |
Output is correct |
53 |
Correct |
83 ms |
1748 KB |
Output is correct |
54 |
Correct |
84 ms |
1876 KB |
Output is correct |
55 |
Correct |
81 ms |
1876 KB |
Output is correct |
56 |
Correct |
28 ms |
596 KB |
Output is correct |
57 |
Correct |
43 ms |
596 KB |
Output is correct |
58 |
Correct |
23 ms |
596 KB |
Output is correct |
59 |
Correct |
15 ms |
596 KB |
Output is correct |
60 |
Correct |
17 ms |
596 KB |
Output is correct |
61 |
Correct |
18 ms |
596 KB |
Output is correct |
62 |
Correct |
23 ms |
724 KB |
Output is correct |
63 |
Correct |
69 ms |
10528 KB |
Output is correct |
64 |
Correct |
77 ms |
12588 KB |
Output is correct |
65 |
Correct |
79 ms |
13304 KB |
Output is correct |
66 |
Correct |
82 ms |
13232 KB |
Output is correct |
67 |
Correct |
20 ms |
596 KB |
Output is correct |
68 |
Correct |
21 ms |
596 KB |
Output is correct |
69 |
Correct |
40 ms |
596 KB |
Output is correct |
70 |
Correct |
37 ms |
596 KB |
Output is correct |
71 |
Correct |
30 ms |
596 KB |
Output is correct |
72 |
Correct |
26 ms |
596 KB |
Output is correct |
73 |
Correct |
35 ms |
596 KB |
Output is correct |
74 |
Correct |
32 ms |
596 KB |
Output is correct |
75 |
Correct |
33 ms |
596 KB |
Output is correct |
76 |
Correct |
31 ms |
596 KB |
Output is correct |
77 |
Correct |
32 ms |
596 KB |
Output is correct |
78 |
Correct |
32 ms |
596 KB |
Output is correct |
79 |
Correct |
34 ms |
596 KB |
Output is correct |
80 |
Correct |
33 ms |
596 KB |
Output is correct |
81 |
Correct |
34 ms |
596 KB |
Output is correct |
82 |
Correct |
32 ms |
596 KB |
Output is correct |
83 |
Correct |
33 ms |
596 KB |
Output is correct |
84 |
Correct |
32 ms |
1888 KB |
Output is correct |
85 |
Correct |
32 ms |
1876 KB |
Output is correct |
86 |
Correct |
86 ms |
1876 KB |
Output is correct |
87 |
Correct |
81 ms |
1748 KB |
Output is correct |
88 |
Correct |
39 ms |
1876 KB |
Output is correct |
89 |
Correct |
78 ms |
1876 KB |
Output is correct |
90 |
Correct |
82 ms |
1876 KB |
Output is correct |
91 |
Correct |
85 ms |
1876 KB |
Output is correct |
92 |
Correct |
83 ms |
1748 KB |
Output is correct |
93 |
Correct |
80 ms |
1748 KB |
Output is correct |
94 |
Correct |
84 ms |
1748 KB |
Output is correct |
95 |
Correct |
86 ms |
1808 KB |
Output is correct |
96 |
Correct |
193 ms |
13264 KB |
Output is correct |
97 |
Correct |
41 ms |
596 KB |
Output is correct |
98 |
Correct |
171 ms |
13356 KB |
Output is correct |
99 |
Correct |
93 ms |
13356 KB |
Output is correct |
100 |
Correct |
32 ms |
596 KB |
Output is correct |
101 |
Correct |
171 ms |
13388 KB |
Output is correct |
102 |
Correct |
194 ms |
13260 KB |
Output is correct |
103 |
Correct |
193 ms |
13332 KB |
Output is correct |
104 |
Correct |
190 ms |
13316 KB |
Output is correct |
105 |
Correct |
193 ms |
13352 KB |
Output is correct |
106 |
Correct |
194 ms |
13344 KB |
Output is correct |
107 |
Correct |
188 ms |
13264 KB |
Output is correct |
108 |
Correct |
187 ms |
13204 KB |
Output is correct |
109 |
Correct |
188 ms |
13324 KB |
Output is correct |