//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...); }
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 100005
#define maxc 15
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const ll inf = 1LL<<60;
ll dp[1<<maxc];
int pref[maxn], suf[maxn], col[maxn];
vector<int> pos[maxc];
ll cnt[maxc][maxc][maxn];
int main() {
io
ll n;
string s;
cin >> s;
n = s.size();
for (int i = 0;i < n;i++) {
int c = s[i] - 'A';
pos[c].push_back(i);
col[i] = c;
}
for (int i = 0;i < maxc;i++) {
for (int j = 0;j < n;j++) {
pref[j] = suf[j] = (col[j] == i) ? 1 : 0;
pref[j] += j ? pref[j-1] : 0;
}
for (int j = n - 2;j >= 0;j--)suf[j] += suf[j+1];
for (int p = 0;p < maxc;p++) {
if (p == i) continue;
ll cur = 0;
for (int x:pos[p]) cur += suf[x];
cnt[p][i][0] = cur;
int id = 1;
for (int x:pos[p]) {
cur += pref[x] - suf[x];
cnt[p][i][id++] = cur;
}
}
}
for (int i = 0;i < (1<<maxc);i++) dp[i] = inf;
dp[0] = 0;
for (int i = 0;i < (1<<maxc);i++) {
vector<int> t;
for (int j = 0;j < maxc;j++) {
if ((i >> j) & 1) t.push_back(j);
}
auto getval = [&] (int p, int k) {
ll ret = 0;
for (int x:t) {
ret += cnt[p][x][k];
}
return ret * 4 + (ll)k * (k - 1) + (ll)(pos[p].size() - k) * (pos[p].size() - k - 1);
};
for (int j = 0;j < maxc;j++) {
if (!((i >> j) & 1)) {
int low = 0, up = pos[j].size()+1;
while (low < up - 2) {
int m1 = (low * 2 + up) / 3, m2 = (low + up * 2) / 3;
if (getval(j, m1) <= getval(j, m2)) up = m2;
else low = m1;
}
ll best = min(getval(j, low), getval(j, (low + up) / 2));
int to = i + (1<<j);
dp[to] = min(dp[to], dp[i] + best);
}
}
}
ll ans = dp[(1<<maxc) - 1];
ll a0 = ans / 4;
cout << a0;
if (ans % 4 == 1) {
cout << ".25\n";
} else if (ans % 4 == 2) {
cout << ".5\n";
} else if (ans % 4 == 3) {
cout << ".75\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
1876 KB |
Output is correct |
2 |
Correct |
16 ms |
1776 KB |
Output is correct |
3 |
Correct |
16 ms |
1780 KB |
Output is correct |
4 |
Correct |
17 ms |
1784 KB |
Output is correct |
5 |
Correct |
28 ms |
1876 KB |
Output is correct |
6 |
Correct |
40 ms |
11920 KB |
Output is correct |
7 |
Correct |
41 ms |
13840 KB |
Output is correct |
8 |
Correct |
47 ms |
14464 KB |
Output is correct |
9 |
Correct |
49 ms |
14564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
1768 KB |
Output is correct |
2 |
Correct |
22 ms |
1748 KB |
Output is correct |
3 |
Correct |
29 ms |
1748 KB |
Output is correct |
4 |
Correct |
27 ms |
1748 KB |
Output is correct |
5 |
Correct |
19 ms |
1796 KB |
Output is correct |
6 |
Correct |
15 ms |
1748 KB |
Output is correct |
7 |
Correct |
26 ms |
1784 KB |
Output is correct |
8 |
Correct |
22 ms |
1780 KB |
Output is correct |
9 |
Correct |
22 ms |
1716 KB |
Output is correct |
10 |
Correct |
30 ms |
1784 KB |
Output is correct |
11 |
Correct |
23 ms |
1748 KB |
Output is correct |
12 |
Correct |
25 ms |
1868 KB |
Output is correct |
13 |
Correct |
21 ms |
1748 KB |
Output is correct |
14 |
Correct |
20 ms |
1784 KB |
Output is correct |
15 |
Correct |
20 ms |
1748 KB |
Output is correct |
16 |
Correct |
21 ms |
1748 KB |
Output is correct |
17 |
Correct |
20 ms |
1748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
1768 KB |
Output is correct |
2 |
Correct |
22 ms |
1748 KB |
Output is correct |
3 |
Correct |
29 ms |
1748 KB |
Output is correct |
4 |
Correct |
27 ms |
1748 KB |
Output is correct |
5 |
Correct |
19 ms |
1796 KB |
Output is correct |
6 |
Correct |
15 ms |
1748 KB |
Output is correct |
7 |
Correct |
26 ms |
1784 KB |
Output is correct |
8 |
Correct |
22 ms |
1780 KB |
Output is correct |
9 |
Correct |
22 ms |
1716 KB |
Output is correct |
10 |
Correct |
30 ms |
1784 KB |
Output is correct |
11 |
Correct |
23 ms |
1748 KB |
Output is correct |
12 |
Correct |
25 ms |
1868 KB |
Output is correct |
13 |
Correct |
21 ms |
1748 KB |
Output is correct |
14 |
Correct |
20 ms |
1784 KB |
Output is correct |
15 |
Correct |
20 ms |
1748 KB |
Output is correct |
16 |
Correct |
21 ms |
1748 KB |
Output is correct |
17 |
Correct |
20 ms |
1748 KB |
Output is correct |
18 |
Correct |
18 ms |
1748 KB |
Output is correct |
19 |
Correct |
22 ms |
1796 KB |
Output is correct |
20 |
Correct |
30 ms |
1748 KB |
Output is correct |
21 |
Correct |
31 ms |
1748 KB |
Output is correct |
22 |
Correct |
20 ms |
1748 KB |
Output is correct |
23 |
Correct |
20 ms |
1784 KB |
Output is correct |
24 |
Correct |
29 ms |
1748 KB |
Output is correct |
25 |
Correct |
23 ms |
1760 KB |
Output is correct |
26 |
Correct |
20 ms |
1748 KB |
Output is correct |
27 |
Correct |
22 ms |
1748 KB |
Output is correct |
28 |
Correct |
21 ms |
1780 KB |
Output is correct |
29 |
Correct |
22 ms |
1748 KB |
Output is correct |
30 |
Correct |
21 ms |
1788 KB |
Output is correct |
31 |
Correct |
20 ms |
1784 KB |
Output is correct |
32 |
Correct |
19 ms |
1748 KB |
Output is correct |
33 |
Correct |
21 ms |
1748 KB |
Output is correct |
34 |
Correct |
21 ms |
1776 KB |
Output is correct |
35 |
Correct |
26 ms |
3120 KB |
Output is correct |
36 |
Correct |
29 ms |
3116 KB |
Output is correct |
37 |
Correct |
79 ms |
3028 KB |
Output is correct |
38 |
Correct |
65 ms |
3028 KB |
Output is correct |
39 |
Correct |
24 ms |
3120 KB |
Output is correct |
40 |
Correct |
71 ms |
3064 KB |
Output is correct |
41 |
Correct |
76 ms |
3064 KB |
Output is correct |
42 |
Correct |
86 ms |
3028 KB |
Output is correct |
43 |
Correct |
75 ms |
3040 KB |
Output is correct |
44 |
Correct |
76 ms |
3028 KB |
Output is correct |
45 |
Correct |
82 ms |
3044 KB |
Output is correct |
46 |
Correct |
82 ms |
3028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
1876 KB |
Output is correct |
2 |
Correct |
16 ms |
1776 KB |
Output is correct |
3 |
Correct |
16 ms |
1780 KB |
Output is correct |
4 |
Correct |
17 ms |
1784 KB |
Output is correct |
5 |
Correct |
28 ms |
1876 KB |
Output is correct |
6 |
Correct |
40 ms |
11920 KB |
Output is correct |
7 |
Correct |
41 ms |
13840 KB |
Output is correct |
8 |
Correct |
47 ms |
14464 KB |
Output is correct |
9 |
Correct |
49 ms |
14564 KB |
Output is correct |
10 |
Correct |
18 ms |
1768 KB |
Output is correct |
11 |
Correct |
22 ms |
1748 KB |
Output is correct |
12 |
Correct |
29 ms |
1748 KB |
Output is correct |
13 |
Correct |
27 ms |
1748 KB |
Output is correct |
14 |
Correct |
19 ms |
1796 KB |
Output is correct |
15 |
Correct |
15 ms |
1748 KB |
Output is correct |
16 |
Correct |
26 ms |
1784 KB |
Output is correct |
17 |
Correct |
22 ms |
1780 KB |
Output is correct |
18 |
Correct |
22 ms |
1716 KB |
Output is correct |
19 |
Correct |
30 ms |
1784 KB |
Output is correct |
20 |
Correct |
23 ms |
1748 KB |
Output is correct |
21 |
Correct |
25 ms |
1868 KB |
Output is correct |
22 |
Correct |
21 ms |
1748 KB |
Output is correct |
23 |
Correct |
20 ms |
1784 KB |
Output is correct |
24 |
Correct |
20 ms |
1748 KB |
Output is correct |
25 |
Correct |
21 ms |
1748 KB |
Output is correct |
26 |
Correct |
20 ms |
1748 KB |
Output is correct |
27 |
Correct |
18 ms |
1748 KB |
Output is correct |
28 |
Correct |
22 ms |
1796 KB |
Output is correct |
29 |
Correct |
30 ms |
1748 KB |
Output is correct |
30 |
Correct |
31 ms |
1748 KB |
Output is correct |
31 |
Correct |
20 ms |
1748 KB |
Output is correct |
32 |
Correct |
20 ms |
1784 KB |
Output is correct |
33 |
Correct |
29 ms |
1748 KB |
Output is correct |
34 |
Correct |
23 ms |
1760 KB |
Output is correct |
35 |
Correct |
20 ms |
1748 KB |
Output is correct |
36 |
Correct |
22 ms |
1748 KB |
Output is correct |
37 |
Correct |
21 ms |
1780 KB |
Output is correct |
38 |
Correct |
22 ms |
1748 KB |
Output is correct |
39 |
Correct |
21 ms |
1788 KB |
Output is correct |
40 |
Correct |
20 ms |
1784 KB |
Output is correct |
41 |
Correct |
19 ms |
1748 KB |
Output is correct |
42 |
Correct |
21 ms |
1748 KB |
Output is correct |
43 |
Correct |
21 ms |
1776 KB |
Output is correct |
44 |
Correct |
26 ms |
3120 KB |
Output is correct |
45 |
Correct |
29 ms |
3116 KB |
Output is correct |
46 |
Correct |
79 ms |
3028 KB |
Output is correct |
47 |
Correct |
65 ms |
3028 KB |
Output is correct |
48 |
Correct |
24 ms |
3120 KB |
Output is correct |
49 |
Correct |
71 ms |
3064 KB |
Output is correct |
50 |
Correct |
76 ms |
3064 KB |
Output is correct |
51 |
Correct |
86 ms |
3028 KB |
Output is correct |
52 |
Correct |
75 ms |
3040 KB |
Output is correct |
53 |
Correct |
76 ms |
3028 KB |
Output is correct |
54 |
Correct |
82 ms |
3044 KB |
Output is correct |
55 |
Correct |
82 ms |
3028 KB |
Output is correct |
56 |
Correct |
19 ms |
1784 KB |
Output is correct |
57 |
Correct |
21 ms |
1748 KB |
Output is correct |
58 |
Correct |
21 ms |
1904 KB |
Output is correct |
59 |
Correct |
19 ms |
1748 KB |
Output is correct |
60 |
Correct |
18 ms |
1784 KB |
Output is correct |
61 |
Correct |
17 ms |
1748 KB |
Output is correct |
62 |
Correct |
23 ms |
1912 KB |
Output is correct |
63 |
Correct |
36 ms |
11920 KB |
Output is correct |
64 |
Correct |
42 ms |
13840 KB |
Output is correct |
65 |
Correct |
44 ms |
14480 KB |
Output is correct |
66 |
Correct |
42 ms |
14520 KB |
Output is correct |
67 |
Correct |
17 ms |
1748 KB |
Output is correct |
68 |
Correct |
19 ms |
1748 KB |
Output is correct |
69 |
Correct |
34 ms |
1784 KB |
Output is correct |
70 |
Correct |
28 ms |
1748 KB |
Output is correct |
71 |
Correct |
19 ms |
1748 KB |
Output is correct |
72 |
Correct |
16 ms |
1748 KB |
Output is correct |
73 |
Correct |
29 ms |
1748 KB |
Output is correct |
74 |
Correct |
22 ms |
1780 KB |
Output is correct |
75 |
Correct |
21 ms |
1748 KB |
Output is correct |
76 |
Correct |
22 ms |
1748 KB |
Output is correct |
77 |
Correct |
21 ms |
1776 KB |
Output is correct |
78 |
Correct |
22 ms |
1748 KB |
Output is correct |
79 |
Correct |
24 ms |
1780 KB |
Output is correct |
80 |
Correct |
23 ms |
1748 KB |
Output is correct |
81 |
Correct |
19 ms |
1780 KB |
Output is correct |
82 |
Correct |
20 ms |
1780 KB |
Output is correct |
83 |
Correct |
24 ms |
1748 KB |
Output is correct |
84 |
Correct |
25 ms |
3096 KB |
Output is correct |
85 |
Correct |
24 ms |
3028 KB |
Output is correct |
86 |
Correct |
74 ms |
3056 KB |
Output is correct |
87 |
Correct |
81 ms |
3048 KB |
Output is correct |
88 |
Correct |
33 ms |
3028 KB |
Output is correct |
89 |
Correct |
80 ms |
3044 KB |
Output is correct |
90 |
Correct |
77 ms |
3048 KB |
Output is correct |
91 |
Correct |
77 ms |
3052 KB |
Output is correct |
92 |
Correct |
86 ms |
3040 KB |
Output is correct |
93 |
Correct |
72 ms |
3028 KB |
Output is correct |
94 |
Correct |
76 ms |
3028 KB |
Output is correct |
95 |
Correct |
79 ms |
3044 KB |
Output is correct |
96 |
Correct |
161 ms |
14480 KB |
Output is correct |
97 |
Correct |
25 ms |
1768 KB |
Output is correct |
98 |
Correct |
141 ms |
14516 KB |
Output is correct |
99 |
Correct |
42 ms |
14480 KB |
Output is correct |
100 |
Correct |
16 ms |
1780 KB |
Output is correct |
101 |
Correct |
135 ms |
14512 KB |
Output is correct |
102 |
Correct |
151 ms |
14548 KB |
Output is correct |
103 |
Correct |
140 ms |
14556 KB |
Output is correct |
104 |
Correct |
150 ms |
14520 KB |
Output is correct |
105 |
Correct |
176 ms |
14532 KB |
Output is correct |
106 |
Correct |
155 ms |
14464 KB |
Output is correct |
107 |
Correct |
138 ms |
14524 KB |
Output is correct |
108 |
Correct |
138 ms |
14540 KB |
Output is correct |
109 |
Correct |
186 ms |
14588 KB |
Output is correct |