#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXG = 15;
const ll MAXN = 1e5 + 10;
const ll INF = 4e18;
int ps[MAXG][MAXN], n;
ll tps[MAXG][MAXG][MAXN], tss[MAXG][MAXG][MAXN], dp[1 << MAXG]; // :)
vector<int> ind[MAXG];
string s;
inline ll check(int mask, int x, int ind) {
ll ans = 0;
for (int i = 0; i < MAXG; i++)
if (mask & (1 << i))
ans += tps[x][i][ind] + tss[x][i][ind + 1];
return ans * 2 + tps[x][x][ind] + tss[x][x][ind + 1];
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> s;
n = s.size();
s = "." + s;
for (int i = 0; i < MAXG; i++) ind[i].push_back(0);
for (int i = 1; i <= n; i++) ind[s[i] - 'A'].push_back(i);
for (int i = 0; i < MAXG; i++)
for (int j = 1; j <= n; j++)
ps[i][j] = ps[i][j - 1] + (int(s[j] - 'A') == i);
for (int i = 0; i < MAXG; i++) {
for (int j = 0; j < MAXG; j++) {
for (int k = 1; k <= n; k++) {
tps[i][j][k] = tps[i][j][k - 1];
if (int(s[k] - 'A') == i)
tps[i][j][k] += ps[j][k - 1];
}
for (int k = n; k > 0; k--) {
tss[i][j][k] = tss[i][j][k + 1];
if (int(s[k] - 'A') == i)
tss[i][j][k] += ps[j][n] - ps[j][k];
}
}
}
for (int mask = 1; mask < (1 << MAXG); mask++) {
dp[mask] = INF;
for (int i = 0; i < MAXG; i++) {
if (mask & (1 << i)) {
int tmask = (mask ^ (1 << i)), l = 0, r = ind[i].size();
while (l < r) {
int mid = (l + r) >> 1;
if (check(tmask, i, ind[i][mid]) >= check(tmask, i, ind[i][mid + 1])) l = mid + 1;
else r = mid;
}
dp[mask] = min(dp[mask], dp[tmask] + check(tmask, i, ind[i][l]));
}
}
}
int mask = (1 << MAXG) - 1;
cout << dp[mask] / 2 << "." << (dp[mask] % 2) * 5 << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
6272 KB |
found '100800.5000000000', expected '100800.5000000000', error '0.0000000000' |
2 |
Correct |
40 ms |
3144 KB |
found '0.0000000000', expected '0.0000000000', error '-0.0000000000' |
3 |
Correct |
43 ms |
3100 KB |
found '0.0000000000', expected '0.0000000000', error '-0.0000000000' |
4 |
Correct |
42 ms |
3036 KB |
found '1.0000000000', expected '1.0000000000', error '0.0000000000' |
5 |
Correct |
52 ms |
6676 KB |
found '124002.0000000000', expected '124002.0000000000', error '0.0000000000' |
6 |
Runtime error |
444 ms |
576368 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
3020 KB |
found '1.0000000000', expected '1.0000000000', error '0.0000000000' |
2 |
Correct |
45 ms |
3436 KB |
found '1225.0000000000', expected '1225.0000000000', error '0.0000000000' |
3 |
Correct |
61 ms |
3424 KB |
found '1023.0000000000', expected '1023.0000000000', error '0.0000000000' |
4 |
Correct |
60 ms |
3380 KB |
found '294.0000000000', expected '294.0000000000', error '0.0000000000' |
5 |
Correct |
51 ms |
3360 KB |
found '1087.0000000000', expected '1087.0000000000', error '0.0000000000' |
6 |
Correct |
47 ms |
3104 KB |
found '1.5000000000', expected '1.5000000000', error '0.0000000000' |
7 |
Correct |
54 ms |
3304 KB |
found '703.0000000000', expected '703.0000000000', error '0.0000000000' |
8 |
Correct |
54 ms |
3148 KB |
found '55.5000000000', expected '55.5000000000', error '0.0000000000' |
9 |
Runtime error |
14 ms |
5448 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
3020 KB |
found '1.0000000000', expected '1.0000000000', error '0.0000000000' |
2 |
Correct |
45 ms |
3436 KB |
found '1225.0000000000', expected '1225.0000000000', error '0.0000000000' |
3 |
Correct |
61 ms |
3424 KB |
found '1023.0000000000', expected '1023.0000000000', error '0.0000000000' |
4 |
Correct |
60 ms |
3380 KB |
found '294.0000000000', expected '294.0000000000', error '0.0000000000' |
5 |
Correct |
51 ms |
3360 KB |
found '1087.0000000000', expected '1087.0000000000', error '0.0000000000' |
6 |
Correct |
47 ms |
3104 KB |
found '1.5000000000', expected '1.5000000000', error '0.0000000000' |
7 |
Correct |
54 ms |
3304 KB |
found '703.0000000000', expected '703.0000000000', error '0.0000000000' |
8 |
Correct |
54 ms |
3148 KB |
found '55.5000000000', expected '55.5000000000', error '0.0000000000' |
9 |
Runtime error |
14 ms |
5448 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
6272 KB |
found '100800.5000000000', expected '100800.5000000000', error '0.0000000000' |
2 |
Correct |
40 ms |
3144 KB |
found '0.0000000000', expected '0.0000000000', error '-0.0000000000' |
3 |
Correct |
43 ms |
3100 KB |
found '0.0000000000', expected '0.0000000000', error '-0.0000000000' |
4 |
Correct |
42 ms |
3036 KB |
found '1.0000000000', expected '1.0000000000', error '0.0000000000' |
5 |
Correct |
52 ms |
6676 KB |
found '124002.0000000000', expected '124002.0000000000', error '0.0000000000' |
6 |
Runtime error |
444 ms |
576368 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |