// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 100005;
const int MAXG = 15;
int n, g;
string s;
int psm[MAXG][MAXN];
ll pre[MAXG + 5][MAXG + 5][MAXN];
ll dp[(1 << MAXG) + 5];
vi vec[MAXG + 5];
inline ll calc(int n) {
return (ll) n * (n - 1) / 2;
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> s;
n = SZ(s);
REP (i, 0, n) {
mxto(g, s[i] - 'A' + 1);
}
REP (i, 0, g) {
REP (j, 0, n) {
if (s[j] - 'A' == i) {
cerr << i << ' ' << j << '\n';
vec[i].pb(j);
psm[i][j] = 1;
}
if (j) {
psm[i][j] += psm[i][j - 1];
}
}
}
REP (j, 0, g) {
REP (i, 0, g) {
int all = 0;
REP (k, 0, n) {
if (s[k] - 'A' == i) {
all++;
}
}
int acnt = 0, ajc = 0;
ll cur = 0;
RREP (k, n - 1, 0) {
if (s[k] - 'A' == j) {
cur += acnt * 2;
ajc++;
} else if (s[k] - 'A' == i) {
acnt++;
}
}
int jc = 0, cnt = 0;
REP (k, 0, n) {
if (s[k] - 'A' == j) {
cur += cnt * 2;
cur -= acnt * 2;
jc++;
} else if (s[k] - 'A' == i) {
cnt++;
acnt--;
}
pre[j][i][k] = cur;
}
}
}
dp[0] = 0;
REP (i, 1, 1 << g) {
dp[i] = LINF;
cerr << bitset<2>(i) << '\n';
REP (j, 0, g) {
if (i >> j & 1 ^ 1) {
continue;
}
cerr << ' ' << j << '\n';
int ni = i ^ (1 << j);
ll mn = LINF;
auto solve = [&] (int k) {
ll tmp = calc(psm[j][k]) + calc(psm[j][n - 1] - psm[j][k]);
REP (l, 0, g) {
if (i >> l & 1 ^ 1) {
continue;
}
tmp += pre[j][l][k];
}
return tmp;
};
if (SZ(vec[j]) == 0) {
mnto(dp[i], dp[ni]);
continue;
}
mnto(dp[i], dp[ni] + solve(0));
int lo = 0, hi = SZ(vec[j]) - 1;
// REP (i, 0, SZ(vec[j])) {
// cerr << " " << i << ' ' << vec[j][i] << ": " << solve(vec[j][i]) << '\n';
// }
while (hi - lo >= 3) {
int mid1 = lo + (hi - lo + 1) / 3, mid2 = mid1 + hi >> 1;
assert(mid1 != mid2);
cerr << " " << lo << ' ' << hi << ' ' << mid1 << ' ' << mid2 << '\n';
ll tmp1 = solve(vec[j][mid1]), tmp2 = solve(vec[j][mid2]);
if (tmp1 <= tmp2) {
hi = mid2;
} else {
lo = mid1;
}
}
REP (k, lo, hi + 1) {
cerr << " " << k << ' ' << vec[j][k] << ": " << solve(vec[j][k]) << '\n';
mnto(dp[i], dp[ni] + solve(vec[j][k]));
}
}
}
ll ans = dp[(1 << g) - 1];
if (ans % 2 == 0) {
ans /= 2;
cout << ans << '\n';
} else {
ans /= 2;
cout << ans << ".5\n";
}
return 0;
}
Compilation message
passes.cpp: In function 'int main()':
passes.cpp:108:24: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
108 | if (i >> j & 1 ^ 1) {
| ~~~~~~~^~~
passes.cpp: In lambda function:
passes.cpp:117:32: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
117 | if (i >> l & 1 ^ 1) {
| ~~~~~~~^~~
passes.cpp: In function 'int main()':
passes.cpp:134:64: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
134 | int mid1 = lo + (hi - lo + 1) / 3, mid2 = mid1 + hi >> 1;
| ~~~~~^~~~
passes.cpp:113:16: warning: unused variable 'mn' [-Wunused-variable]
113 | ll mn = LINF;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
1808 KB |
Output is correct |
7 |
Correct |
2 ms |
2064 KB |
Output is correct |
8 |
Correct |
2 ms |
2192 KB |
Output is correct |
9 |
Correct |
2 ms |
2192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
0 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
0 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
0 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
0 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
5 ms |
6100 KB |
Output is correct |
36 |
Correct |
6 ms |
6100 KB |
Output is correct |
37 |
Correct |
13 ms |
9196 KB |
Output is correct |
38 |
Correct |
12 ms |
9172 KB |
Output is correct |
39 |
Correct |
8 ms |
9284 KB |
Output is correct |
40 |
Correct |
10 ms |
9172 KB |
Output is correct |
41 |
Correct |
9 ms |
8968 KB |
Output is correct |
42 |
Correct |
10 ms |
9044 KB |
Output is correct |
43 |
Correct |
9 ms |
9048 KB |
Output is correct |
44 |
Correct |
9 ms |
9044 KB |
Output is correct |
45 |
Correct |
10 ms |
9044 KB |
Output is correct |
46 |
Correct |
9 ms |
9056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
1808 KB |
Output is correct |
7 |
Correct |
2 ms |
2064 KB |
Output is correct |
8 |
Correct |
2 ms |
2192 KB |
Output is correct |
9 |
Correct |
2 ms |
2192 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
0 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
0 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
0 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
5 ms |
6100 KB |
Output is correct |
45 |
Correct |
6 ms |
6100 KB |
Output is correct |
46 |
Correct |
13 ms |
9196 KB |
Output is correct |
47 |
Correct |
12 ms |
9172 KB |
Output is correct |
48 |
Correct |
8 ms |
9284 KB |
Output is correct |
49 |
Correct |
10 ms |
9172 KB |
Output is correct |
50 |
Correct |
9 ms |
8968 KB |
Output is correct |
51 |
Correct |
10 ms |
9044 KB |
Output is correct |
52 |
Correct |
9 ms |
9048 KB |
Output is correct |
53 |
Correct |
9 ms |
9044 KB |
Output is correct |
54 |
Correct |
10 ms |
9044 KB |
Output is correct |
55 |
Correct |
9 ms |
9056 KB |
Output is correct |
56 |
Correct |
19 ms |
1884 KB |
Output is correct |
57 |
Correct |
30 ms |
1876 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
0 ms |
212 KB |
Output is correct |
62 |
Correct |
0 ms |
284 KB |
Output is correct |
63 |
Correct |
2 ms |
1808 KB |
Output is correct |
64 |
Correct |
2 ms |
2064 KB |
Output is correct |
65 |
Correct |
2 ms |
2192 KB |
Output is correct |
66 |
Correct |
2 ms |
2192 KB |
Output is correct |
67 |
Correct |
0 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
1 ms |
596 KB |
Output is correct |
70 |
Correct |
1 ms |
596 KB |
Output is correct |
71 |
Correct |
1 ms |
596 KB |
Output is correct |
72 |
Correct |
1 ms |
596 KB |
Output is correct |
73 |
Correct |
1 ms |
596 KB |
Output is correct |
74 |
Correct |
1 ms |
596 KB |
Output is correct |
75 |
Correct |
1 ms |
596 KB |
Output is correct |
76 |
Correct |
1 ms |
596 KB |
Output is correct |
77 |
Correct |
1 ms |
596 KB |
Output is correct |
78 |
Correct |
0 ms |
596 KB |
Output is correct |
79 |
Correct |
1 ms |
596 KB |
Output is correct |
80 |
Correct |
1 ms |
596 KB |
Output is correct |
81 |
Correct |
0 ms |
596 KB |
Output is correct |
82 |
Correct |
0 ms |
596 KB |
Output is correct |
83 |
Correct |
0 ms |
596 KB |
Output is correct |
84 |
Correct |
4 ms |
6100 KB |
Output is correct |
85 |
Correct |
4 ms |
6100 KB |
Output is correct |
86 |
Correct |
12 ms |
9172 KB |
Output is correct |
87 |
Correct |
9 ms |
9144 KB |
Output is correct |
88 |
Correct |
6 ms |
9172 KB |
Output is correct |
89 |
Correct |
12 ms |
9172 KB |
Output is correct |
90 |
Correct |
10 ms |
9044 KB |
Output is correct |
91 |
Correct |
12 ms |
9044 KB |
Output is correct |
92 |
Correct |
10 ms |
9044 KB |
Output is correct |
93 |
Correct |
10 ms |
9044 KB |
Output is correct |
94 |
Correct |
10 ms |
8940 KB |
Output is correct |
95 |
Correct |
9 ms |
8988 KB |
Output is correct |
96 |
Correct |
618 ms |
183276 KB |
Output is correct |
97 |
Correct |
22 ms |
1620 KB |
Output is correct |
98 |
Correct |
351 ms |
183352 KB |
Output is correct |
99 |
Correct |
173 ms |
183408 KB |
Output is correct |
100 |
Correct |
28 ms |
2004 KB |
Output is correct |
101 |
Correct |
453 ms |
183408 KB |
Output is correct |
102 |
Correct |
545 ms |
183504 KB |
Output is correct |
103 |
Correct |
540 ms |
183256 KB |
Output is correct |
104 |
Correct |
521 ms |
183260 KB |
Output is correct |
105 |
Correct |
552 ms |
183384 KB |
Output is correct |
106 |
Correct |
552 ms |
183276 KB |
Output is correct |
107 |
Correct |
541 ms |
183384 KB |
Output is correct |
108 |
Correct |
285 ms |
160036 KB |
Output is correct |
109 |
Correct |
545 ms |
183420 KB |
Output is correct |