#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ll exp_inv(ll x) {
return x * (x-1);
}
int main() {
char a = 'A';
string s; cin >> s;
const int G = 10;
int n = s.size();
if (n == 1) {
cout << "0\n";
return 0;
}
vector memo(1<<G, (ld)-1);
auto suml = [&](int c1, int c2, int l, int r) {
ll cur = 0, res = 0;
for (int i = l; i <= r; i++) {
if (s[i] == c1+a) res += cur;
if (s[i] == c2+a) cur++;
}
return res;
};
auto sumr = [&](int c1, int c2, int l, int r) {
ll cur = 0, res = 0;
for (int i = r; i >= l; i--) {
if (s[i] == c1+a) res += cur;
if (s[i] == c2+a) cur++;
}
return res;
};
auto dp = [&](auto& dp, int used)->ld{
if (memo[used] >= 0) return memo[used];
if (__builtin_popcount(used) == G) return 0;
vector has(n, 0);
for (int j = 0; j < n; j++) {
if (used& 1<<(s[j] - a))
has[j] = 1;
}
ld res = 1e18;
for (int i = 0 ;i < G; i++) {
ld cur = 1e18;
if (used&1<<i) continue;
vector<ld> left(n), right(n);
int c = 0;
for (int j = 0; j < n; j++) {
if (s[j] == i + a)
c++;
ll sum = 0;
for (int c2 = 0; c2 < G; c2++)
if (used&1<<i)
sum+= suml(i, c2, 0, j);
left[j] = sum * 4 + exp_inv(c);
}
c = 0;
for (int j = n-1; j >= 0; j--) {
if (s[j] == i + a)
c++;
ll sum = 0;
for (int c2 = 0; c2 < G; c2++)
if (used&1<<i)
sum += sumr(i, c2, j, n-1);
right[j] = sum * 4 + exp_inv(c);
}
for (int mid = 1; mid < n; mid++) {
cur = min(cur, left[mid-1] + right[mid]);
}
res = min(res, cur + dp(dp, used |1<<i));
}
return memo[used] = res;
};
ll ans;
if (n > 10000) ans = exp_inv(n / 2) + exp_inv(n - n / 2);
else ans = dp(dp, 0);
cout << ans / 4;
if (ans%4 == 1) cout << ".25";
if (ans%4 == 2) cout << ".5";
if (ans%4== 3) cout << ".75";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
596 KB |
found '100800.5000000000', expected '100800.5000000000', error '0.0000000000' |
2 |
Correct |
0 ms |
212 KB |
found '0.0000000000', expected '0.0000000000', error '-0.0000000000' |
3 |
Correct |
1 ms |
212 KB |
found '0.0000000000', expected '0.0000000000', error '-0.0000000000' |
4 |
Correct |
1 ms |
212 KB |
found '1.0000000000', expected '1.0000000000', error '0.0000000000' |
5 |
Correct |
90 ms |
664 KB |
found '124002.0000000000', expected '124002.0000000000', error '0.0000000000' |
6 |
Correct |
3 ms |
408 KB |
found '772893586.0000000000', expected '772893586.0000000000', error '0.0000000000' |
7 |
Correct |
3 ms |
468 KB |
found '1100977812.5000000000', expected '1100977812.5000000000', error '0.0000000000' |
8 |
Correct |
3 ms |
468 KB |
found '1249950000.5000000000', expected '1249950000.5000000000', error '0.0000000000' |
9 |
Correct |
3 ms |
508 KB |
found '1249975000.0000000000', expected '1249975000.0000000000', error '0.0000000000' |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
found '1.0000000000', expected '1.0000000000', error '0.0000000000' |
2 |
Correct |
11 ms |
348 KB |
found '1225.0000000000', expected '1225.0000000000', error '0.0000000000' |
3 |
Incorrect |
10 ms |
348 KB |
1st numbers differ - expected: '1023.0000000000', found: '165.5000000000', error = '0.8382209189' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
found '1.0000000000', expected '1.0000000000', error '0.0000000000' |
2 |
Correct |
11 ms |
348 KB |
found '1225.0000000000', expected '1225.0000000000', error '0.0000000000' |
3 |
Incorrect |
10 ms |
348 KB |
1st numbers differ - expected: '1023.0000000000', found: '165.5000000000', error = '0.8382209189' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
596 KB |
found '100800.5000000000', expected '100800.5000000000', error '0.0000000000' |
2 |
Correct |
0 ms |
212 KB |
found '0.0000000000', expected '0.0000000000', error '-0.0000000000' |
3 |
Correct |
1 ms |
212 KB |
found '0.0000000000', expected '0.0000000000', error '-0.0000000000' |
4 |
Correct |
1 ms |
212 KB |
found '1.0000000000', expected '1.0000000000', error '0.0000000000' |
5 |
Correct |
90 ms |
664 KB |
found '124002.0000000000', expected '124002.0000000000', error '0.0000000000' |
6 |
Correct |
3 ms |
408 KB |
found '772893586.0000000000', expected '772893586.0000000000', error '0.0000000000' |
7 |
Correct |
3 ms |
468 KB |
found '1100977812.5000000000', expected '1100977812.5000000000', error '0.0000000000' |
8 |
Correct |
3 ms |
468 KB |
found '1249950000.5000000000', expected '1249950000.5000000000', error '0.0000000000' |
9 |
Correct |
3 ms |
508 KB |
found '1249975000.0000000000', expected '1249975000.0000000000', error '0.0000000000' |
10 |
Correct |
1 ms |
212 KB |
found '1.0000000000', expected '1.0000000000', error '0.0000000000' |
11 |
Correct |
11 ms |
348 KB |
found '1225.0000000000', expected '1225.0000000000', error '0.0000000000' |
12 |
Incorrect |
10 ms |
348 KB |
1st numbers differ - expected: '1023.0000000000', found: '165.5000000000', error = '0.8382209189' |
13 |
Halted |
0 ms |
0 KB |
- |