/*
* File created on 07/19/2022 at 18:21:46.
* Link to problem:
* Description:
* Time complexity: O()
* Space complexity: O()
* Status: ---
* Copyright: Ⓒ 2022 Francois Vogel
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define pii pair<int, int>
#define f first
#define s second
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define size(x) (int)((x).size())
#define pb push_back
#define ins insert
#define cls clear
#define int ll
#define ll long long
#define ld long double
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int inf = 1e18;
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout.precision(20);
string b;
cin >> b;
const int n = size(b);
int gK = 0;
for (char i : b) if (i-'A'+1 > gK) gK = i-'A'+1;
vi per [gK];
for (int i = 0; i < n; i++) {
per[b[i]-'A'].pb(i);
}
ld cost [gK][1<<gK];
for (int i = 0; i < gK; i++) for (int j = 0; j < (1<<gK); j++) if (!((j>>i)&1)) {
int pref [n];
for (int k = 0; k < n; k++) pref[k] = (j>>(b[k]-'A'))&1;
for (int k = 1; k < n; k++) pref[k] += pref[k-1];
cost[i][j] = 0;
for (int k = 0; k < size(per[i]); k++) {
ld dl = k;
ld dr = size(per[i])-k-1;
ld cl = 0;
if (per[i][k]) cl = pref[per[i][k]-1];
ld cr = pref[n-1]-pref[per[i][k]];
cost[i][j] += min(cl+dl/(ld)(2), cr+dr/(ld)(2));
}
}
ld dp [1<<gK];
dp[(1<<gK)-1] = 0;
for (int i = (1<<gK)-2; i >= 0; i--) {
dp[i] = inf;
for (int j = 0; j < gK; j++) if (!((i>>j)&1)) {
dp[i] = min(dp[i], dp[i|(1<<j)]+cost[j][i]);
}
}
cout << dp[0];
cout.flush();
int d = 0;
d++;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
1804 KB |
Output is correct |
7 |
Correct |
3 ms |
2060 KB |
Output is correct |
8 |
Correct |
3 ms |
2188 KB |
Output is correct |
9 |
Correct |
3 ms |
2188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
260 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
38 ms |
636 KB |
Output is correct |
36 |
Correct |
40 ms |
652 KB |
Output is correct |
37 |
Correct |
197 ms |
724 KB |
Output is correct |
38 |
Correct |
179 ms |
596 KB |
Output is correct |
39 |
Correct |
180 ms |
776 KB |
Output is correct |
40 |
Correct |
192 ms |
664 KB |
Output is correct |
41 |
Correct |
177 ms |
708 KB |
Output is correct |
42 |
Correct |
178 ms |
640 KB |
Output is correct |
43 |
Correct |
177 ms |
724 KB |
Output is correct |
44 |
Correct |
175 ms |
688 KB |
Output is correct |
45 |
Correct |
185 ms |
692 KB |
Output is correct |
46 |
Correct |
179 ms |
684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
1804 KB |
Output is correct |
7 |
Correct |
3 ms |
2060 KB |
Output is correct |
8 |
Correct |
3 ms |
2188 KB |
Output is correct |
9 |
Correct |
3 ms |
2188 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
260 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
38 ms |
636 KB |
Output is correct |
45 |
Correct |
40 ms |
652 KB |
Output is correct |
46 |
Correct |
197 ms |
724 KB |
Output is correct |
47 |
Correct |
179 ms |
596 KB |
Output is correct |
48 |
Correct |
180 ms |
776 KB |
Output is correct |
49 |
Correct |
192 ms |
664 KB |
Output is correct |
50 |
Correct |
177 ms |
708 KB |
Output is correct |
51 |
Correct |
178 ms |
640 KB |
Output is correct |
52 |
Correct |
177 ms |
724 KB |
Output is correct |
53 |
Correct |
175 ms |
688 KB |
Output is correct |
54 |
Correct |
185 ms |
692 KB |
Output is correct |
55 |
Correct |
179 ms |
684 KB |
Output is correct |
56 |
Correct |
16 ms |
8520 KB |
Output is correct |
57 |
Correct |
35 ms |
8380 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
316 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
3 ms |
1932 KB |
Output is correct |
64 |
Correct |
3 ms |
2188 KB |
Output is correct |
65 |
Correct |
3 ms |
2316 KB |
Output is correct |
66 |
Correct |
3 ms |
2308 KB |
Output is correct |
67 |
Correct |
1 ms |
212 KB |
Output is correct |
68 |
Correct |
0 ms |
212 KB |
Output is correct |
69 |
Correct |
1 ms |
212 KB |
Output is correct |
70 |
Correct |
1 ms |
320 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
340 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
340 KB |
Output is correct |
78 |
Correct |
1 ms |
320 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
1 ms |
212 KB |
Output is correct |
81 |
Correct |
1 ms |
212 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |
84 |
Correct |
38 ms |
596 KB |
Output is correct |
85 |
Correct |
38 ms |
648 KB |
Output is correct |
86 |
Correct |
180 ms |
712 KB |
Output is correct |
87 |
Correct |
191 ms |
652 KB |
Output is correct |
88 |
Correct |
177 ms |
772 KB |
Output is correct |
89 |
Correct |
182 ms |
656 KB |
Output is correct |
90 |
Correct |
185 ms |
704 KB |
Output is correct |
91 |
Correct |
177 ms |
696 KB |
Output is correct |
92 |
Correct |
179 ms |
704 KB |
Output is correct |
93 |
Correct |
184 ms |
700 KB |
Output is correct |
94 |
Correct |
179 ms |
712 KB |
Output is correct |
95 |
Correct |
176 ms |
692 KB |
Output is correct |
96 |
Execution timed out |
2058 ms |
10032 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |