// 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=(1e4)+10;
int n,N;
char s[maxn*10],lsh[maxn*10];
int m,pre[1<<10][maxn],suf[1<<10][maxn];
vector<int> vec[30];
ll dp[1<<10];
void output(ll ans) {
if (ans&1) printf("%lld.5\n",ans/2);
else printf("%lld\n",ans/2);
exit(0);
}
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=1;i<=n;i++) pre[t][i]=pre[t][i-1]+(t>>(s[i]-'A')&1);
for (int i=n;i>=1;i--) suf[t][i]=suf[t][i+1]+(t>>(s[i]-'A')&1);
dp[t]=INF;
}
dp[0]=0;
for (int t=0;t<(1<<m);t++) {
for (int i=0;i<m;i++) if (!(t>>i&1)) {
ll tmp=0;
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:30:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | 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 |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 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 |
4 ms |
1096 KB |
Output is correct |
7 |
Correct |
3 ms |
1104 KB |
Output is correct |
8 |
Correct |
5 ms |
1152 KB |
Output is correct |
9 |
Correct |
5 ms |
1104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
1364 KB |
Output is correct |
4 |
Correct |
1 ms |
1364 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
868 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
1364 KB |
Output is correct |
4 |
Correct |
1 ms |
1364 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
868 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
1364 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
1 ms |
1364 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
2 ms |
1364 KB |
Output is correct |
28 |
Correct |
1 ms |
1364 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
1364 KB |
Output is correct |
32 |
Correct |
1 ms |
1364 KB |
Output is correct |
33 |
Correct |
1 ms |
1364 KB |
Output is correct |
34 |
Correct |
1 ms |
1364 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
101 ms |
80560 KB |
Output is correct |
38 |
Correct |
105 ms |
80596 KB |
Output is correct |
39 |
Correct |
116 ms |
80588 KB |
Output is correct |
40 |
Correct |
102 ms |
80588 KB |
Output is correct |
41 |
Correct |
104 ms |
80544 KB |
Output is correct |
42 |
Correct |
99 ms |
80552 KB |
Output is correct |
43 |
Correct |
111 ms |
80664 KB |
Output is correct |
44 |
Correct |
106 ms |
80588 KB |
Output is correct |
45 |
Correct |
109 ms |
80544 KB |
Output is correct |
46 |
Correct |
110 ms |
80704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 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 |
4 ms |
1096 KB |
Output is correct |
7 |
Correct |
3 ms |
1104 KB |
Output is correct |
8 |
Correct |
5 ms |
1152 KB |
Output is correct |
9 |
Correct |
5 ms |
1104 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
868 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
1364 KB |
Output is correct |
20 |
Correct |
1 ms |
1364 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
1 ms |
1364 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1364 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
1364 KB |
Output is correct |
32 |
Correct |
1 ms |
1364 KB |
Output is correct |
33 |
Correct |
1 ms |
1364 KB |
Output is correct |
34 |
Correct |
1 ms |
852 KB |
Output is correct |
35 |
Correct |
1 ms |
1364 KB |
Output is correct |
36 |
Correct |
2 ms |
1364 KB |
Output is correct |
37 |
Correct |
1 ms |
1364 KB |
Output is correct |
38 |
Correct |
1 ms |
1364 KB |
Output is correct |
39 |
Correct |
1 ms |
1364 KB |
Output is correct |
40 |
Correct |
1 ms |
1364 KB |
Output is correct |
41 |
Correct |
1 ms |
1364 KB |
Output is correct |
42 |
Correct |
1 ms |
1364 KB |
Output is correct |
43 |
Correct |
1 ms |
1364 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
101 ms |
80560 KB |
Output is correct |
47 |
Correct |
105 ms |
80596 KB |
Output is correct |
48 |
Correct |
116 ms |
80588 KB |
Output is correct |
49 |
Correct |
102 ms |
80588 KB |
Output is correct |
50 |
Correct |
104 ms |
80544 KB |
Output is correct |
51 |
Correct |
99 ms |
80552 KB |
Output is correct |
52 |
Correct |
111 ms |
80664 KB |
Output is correct |
53 |
Correct |
106 ms |
80588 KB |
Output is correct |
54 |
Correct |
109 ms |
80544 KB |
Output is correct |
55 |
Correct |
110 ms |
80704 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Runtime error |
39 ms |
17876 KB |
Execution killed with signal 11 |
58 |
Halted |
0 ms |
0 KB |
- |