// wygzgyw
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
template <typename T> void write(T t) {
if (t<0) { putchar('-'); write(-t); return; }
if (t>9) write(t/10);
putchar('0'+t%10);
}
template <typename T> void writeln(T t) { write(t); puts(""); }
#define MP make_pair
typedef long long ll;
const ll INF=1e18;
const int maxn=(1e5)+10;
int n,N,m;
char s[maxn],lsh[maxn];
vector<int> vec[30];
ll dp[1<<15];
vector<ll> pre[16][16],suf[16][16];
int sz[1<<15];
void output(ll ans) {
if (ans&1) printf("%lld.5\n",ans/2);
else printf("%lld\n",ans/2);
exit(0);
}
int calc(int i,int x) {
x=lower_bound(vec[i].begin(),vec[i].end(),x)-vec[i].begin();
return x;
}
ll S(ll x) { return x*(x+1)/2; }
int main() {
scanf("%s",s+1); n=strlen(s+1);
for (int i=1;i<=n;i++) lsh[++N]=s[i];
sort(lsh+1,lsh+N+1),N=unique(lsh+1,lsh+N+1)-lsh-1;
for (int i=1;i<=n;i++) s[i]=lower_bound(lsh+1,lsh+N+1,s[i])-lsh+'A'-1;
for (int i=1;i<=n;i++) m=max(m,s[i]-'A'),vec[s[i]-'A'].push_back(i);
if (m==0) {
m=n/2;
ll ans=(ll)m*(m-1)/2+(ll)(n-m)*(n-m-1)/2;
output(ans);
}
m++;
for (int t=0;t<(1<<m);t++) {
for (int i=0;i<m;i++) if (t>>i&1) sz[t]+=(int)vec[i].size();
dp[t]=INF;
}
for (int j=0;j<m;j++) for (int i=0;i<m;i++) if (i!=j) {
int n1=0; ll n2=0; int pos=0;
pre[j][i].resize(vec[i].size());
suf[j][i].resize(vec[i].size());
for (int x=1;x<=n;x++) {
if (j==s[x]-'A') n1++;
if (i==s[x]-'A') n2+=n1,pre[j][i][pos]=n2,pos++;
}
n1=0,n2=0;
for (int x=n;x>=1;x--) {
if (j==s[x]-'A') n1++;
if (i==s[x]-'A') n2+=n1,pos--,suf[j][i][pos]=n2;
}
}
dp[0]=0;
for (int t=0;t<(1<<m);t++) {
for (int i=0;i<m;i++) if (!(t>>i&1)) {
ll all=sz[t]*2+sz[1<<i]-1;
ll tmp=0;
int l=0,r=(int)vec[i].size()-1,mid,res=-1;
while (l<=r) {
mid=(l+r)>>1;
int x=vec[i][mid];
int zuo=mid;
for (int j=0;j<m;j++) if (t>>j&1) zuo+=2*calc(j,x);
int you=all-zuo;
if (zuo<=you) res=mid,l=mid+1; else r=mid-1;
}
if (res>=0) { for (int j=0;j<m;j++) if (t>>j&1) tmp+=2*pre[j][i][res]; tmp+=S(res); }
if (res<(int)vec[i].size()-1) { for (int j=0;j<m;j++) if (t>>j&1) tmp+=2*suf[j][i][res+1]; tmp+=S((int)vec[i].size()-res-2); }
//for (int &x : vec[i]) tmp+=min(pre[t][x]*2+pre[1<<i][x-1],suf[t][x]*2+suf[1<<i][x+1]);
dp[t^(1<<i)]=min(dp[t^(1<<i)],dp[t]+tmp);
}
}
output(dp[(1<<m)-1]);
return 0;
}
/*
0. Enough array size? Enough array size? Enough array size? Integer overflow?
1. Think TWICE, Code ONCE!
Are there any counterexamples to your algo?
2. Be careful about the BOUNDARIES!
N=1? P=1? Something about 0?
3. Do not make STUPID MISTAKES!
Time complexity? Memory usage? Precision error?
*/
Compilation message
passes.cpp: In function 'int main()':
passes.cpp:36:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf("%s",s+1); n=strlen(s+1);
| ~~~~~^~~~~~~~~~
# |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
1104 KB |
Output is correct |
7 |
Correct |
3 ms |
1104 KB |
Output is correct |
8 |
Correct |
3 ms |
1104 KB |
Output is correct |
9 |
Correct |
4 ms |
1104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 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 |
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 |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
17 ms |
1832 KB |
Output is correct |
38 |
Correct |
6 ms |
1748 KB |
Output is correct |
39 |
Correct |
4 ms |
1748 KB |
Output is correct |
40 |
Correct |
8 ms |
1748 KB |
Output is correct |
41 |
Correct |
12 ms |
1828 KB |
Output is correct |
42 |
Correct |
13 ms |
1720 KB |
Output is correct |
43 |
Correct |
13 ms |
1824 KB |
Output is correct |
44 |
Correct |
14 ms |
1748 KB |
Output is correct |
45 |
Correct |
13 ms |
1820 KB |
Output is correct |
46 |
Correct |
14 ms |
1692 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
1104 KB |
Output is correct |
7 |
Correct |
3 ms |
1104 KB |
Output is correct |
8 |
Correct |
3 ms |
1104 KB |
Output is correct |
9 |
Correct |
4 ms |
1104 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
3 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 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 |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
17 ms |
1832 KB |
Output is correct |
47 |
Correct |
6 ms |
1748 KB |
Output is correct |
48 |
Correct |
4 ms |
1748 KB |
Output is correct |
49 |
Correct |
8 ms |
1748 KB |
Output is correct |
50 |
Correct |
12 ms |
1828 KB |
Output is correct |
51 |
Correct |
13 ms |
1720 KB |
Output is correct |
52 |
Correct |
13 ms |
1824 KB |
Output is correct |
53 |
Correct |
14 ms |
1748 KB |
Output is correct |
54 |
Correct |
13 ms |
1820 KB |
Output is correct |
55 |
Correct |
14 ms |
1692 KB |
Output is correct |
56 |
Correct |
0 ms |
212 KB |
Output is correct |
57 |
Correct |
68 ms |
700 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
0 ms |
212 KB |
Output is correct |
62 |
Correct |
0 ms |
340 KB |
Output is correct |
63 |
Correct |
3 ms |
1104 KB |
Output is correct |
64 |
Correct |
3 ms |
1104 KB |
Output is correct |
65 |
Correct |
4 ms |
1104 KB |
Output is correct |
66 |
Correct |
4 ms |
1104 KB |
Output is correct |
67 |
Correct |
0 ms |
212 KB |
Output is correct |
68 |
Correct |
0 ms |
212 KB |
Output is correct |
69 |
Correct |
1 ms |
340 KB |
Output is correct |
70 |
Correct |
0 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
340 KB |
Output is correct |
74 |
Correct |
1 ms |
340 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
17 ms |
1748 KB |
Output is correct |
87 |
Correct |
6 ms |
1748 KB |
Output is correct |
88 |
Correct |
4 ms |
1748 KB |
Output is correct |
89 |
Correct |
8 ms |
1824 KB |
Output is correct |
90 |
Correct |
12 ms |
1824 KB |
Output is correct |
91 |
Correct |
14 ms |
1812 KB |
Output is correct |
92 |
Correct |
13 ms |
1820 KB |
Output is correct |
93 |
Correct |
13 ms |
1748 KB |
Output is correct |
94 |
Correct |
15 ms |
1820 KB |
Output is correct |
95 |
Correct |
13 ms |
1820 KB |
Output is correct |
96 |
Correct |
1257 ms |
23400 KB |
Output is correct |
97 |
Correct |
29 ms |
540 KB |
Output is correct |
98 |
Correct |
433 ms |
23324 KB |
Output is correct |
99 |
Correct |
105 ms |
23340 KB |
Output is correct |
100 |
Correct |
35 ms |
728 KB |
Output is correct |
101 |
Correct |
551 ms |
23404 KB |
Output is correct |
102 |
Correct |
947 ms |
23328 KB |
Output is correct |
103 |
Correct |
931 ms |
23256 KB |
Output is correct |
104 |
Correct |
932 ms |
23364 KB |
Output is correct |
105 |
Correct |
936 ms |
23268 KB |
Output is correct |
106 |
Correct |
908 ms |
23264 KB |
Output is correct |
107 |
Correct |
923 ms |
23236 KB |
Output is correct |
108 |
Correct |
446 ms |
21464 KB |
Output is correct |
109 |
Correct |
949 ms |
23500 KB |
Output is correct |