/*input
HEHEHEHIHILOL
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
//#pragma GCC target("avx2")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<ll,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET(v,x) lower_bound(ALL(v),x)-v.begin()
const ll maxn=1e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-6;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
out<<P.f<<' '<<P.s;
return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
REP(i,sz(V)) out<<V[i]<<((i!=sz(V)-1)?"\n":"\n");
return out;
}
ll mult(ll a,ll b){
return a*b%MOD;
}
ll mypow(ll a,ll b){
a%=MOD;
if(a==0) return 0;
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=(res*a)%MOD;
a=(a*a)%MOD;
b>>=1;
}
return res;
}
const int cc=15;
ll dp[(1<<cc)];
ll pf[20][maxn],sf[20][maxn];
vector<int> v[20];
ll get(int m,int i,ll p){
ll tot=0;
REP(j,cc){
if(m&(1<<j)){
tot+=pf[j][v[i][p]]+sf[j][v[i][p+1]];
}
}
ll k=sz(v[i])-p-2;
return tot*2+p*(p-1)/2+k*(k-1)/2;
}
int main(){
ios::sync_with_stdio(false),cin.tie(0);
string s;
cin>>s;
int n=sz(s);
REP(i,cc) v[i].pb(0);
REP(i,n) v[s[i]-'A'].pb(i+1);
REP(i,cc) v[i].pb(n+1);
REP(i,cc){
REP1(j,n){
pf[i][j]=lower_bound(ALL(v[i]),j)-v[i].begin()-1;
sf[i][j]=sz(v[i])-pf[i][j]-2;
}
}
REP(k,cc){
REP(i,cc){
REP1(j,sz(v[i])-1){
pf[k][v[i][j]]+=pf[k][v[i][j-1]];
}
for(int j=sz(v[i])-2;j>=0;j--){
sf[k][v[i][j]]+=sf[k][v[i][j+1]];
}
}
}
REP1(i,(1<<cc)-1){
dp[i]=INF64;
REP(j,cc){
if(i&(1<<j)){
if(sz(v[j])==2){
MNTO(dp[i],dp[i^(1<<j)]);
continue;
}
int z=(i^(1<<j));
int l=0,r=sz(v[j])-2;
while(r-l>2){
int m1=(r-l)/3+l,m2=r-(r-l)/3;
if(get(z,j,m1)>get(z,j,m2)) l=m1+1;
else r=m2;
}
ll ans=INF64;
for(int k=l;k<=r;k++) MNTO(ans,get(z,j,k));
MNTO(dp[i],dp[i^(1<<j)]+ans);
}
}
}
ll z=dp[(1<<cc)-1];
cout<<z/2;
if(z%2) cout<<".5";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
852 KB |
Output is correct |
2 |
Correct |
5 ms |
704 KB |
Output is correct |
3 |
Correct |
5 ms |
696 KB |
Output is correct |
4 |
Correct |
8 ms |
724 KB |
Output is correct |
5 |
Correct |
21 ms |
864 KB |
Output is correct |
6 |
Correct |
54 ms |
19788 KB |
Output is correct |
7 |
Correct |
60 ms |
23420 KB |
Output is correct |
8 |
Correct |
60 ms |
24792 KB |
Output is correct |
9 |
Correct |
64 ms |
24836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
744 KB |
Output is correct |
2 |
Correct |
15 ms |
724 KB |
Output is correct |
3 |
Correct |
54 ms |
708 KB |
Output is correct |
4 |
Correct |
52 ms |
644 KB |
Output is correct |
5 |
Correct |
28 ms |
748 KB |
Output is correct |
6 |
Correct |
18 ms |
724 KB |
Output is correct |
7 |
Correct |
53 ms |
680 KB |
Output is correct |
8 |
Correct |
36 ms |
716 KB |
Output is correct |
9 |
Correct |
33 ms |
700 KB |
Output is correct |
10 |
Correct |
31 ms |
720 KB |
Output is correct |
11 |
Correct |
28 ms |
712 KB |
Output is correct |
12 |
Correct |
30 ms |
628 KB |
Output is correct |
13 |
Correct |
32 ms |
696 KB |
Output is correct |
14 |
Correct |
37 ms |
648 KB |
Output is correct |
15 |
Correct |
36 ms |
732 KB |
Output is correct |
16 |
Correct |
36 ms |
716 KB |
Output is correct |
17 |
Correct |
32 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
744 KB |
Output is correct |
2 |
Correct |
15 ms |
724 KB |
Output is correct |
3 |
Correct |
54 ms |
708 KB |
Output is correct |
4 |
Correct |
52 ms |
644 KB |
Output is correct |
5 |
Correct |
28 ms |
748 KB |
Output is correct |
6 |
Correct |
18 ms |
724 KB |
Output is correct |
7 |
Correct |
53 ms |
680 KB |
Output is correct |
8 |
Correct |
36 ms |
716 KB |
Output is correct |
9 |
Correct |
33 ms |
700 KB |
Output is correct |
10 |
Correct |
31 ms |
720 KB |
Output is correct |
11 |
Correct |
28 ms |
712 KB |
Output is correct |
12 |
Correct |
30 ms |
628 KB |
Output is correct |
13 |
Correct |
32 ms |
696 KB |
Output is correct |
14 |
Correct |
37 ms |
648 KB |
Output is correct |
15 |
Correct |
36 ms |
732 KB |
Output is correct |
16 |
Correct |
36 ms |
716 KB |
Output is correct |
17 |
Correct |
32 ms |
716 KB |
Output is correct |
18 |
Correct |
9 ms |
724 KB |
Output is correct |
19 |
Correct |
15 ms |
644 KB |
Output is correct |
20 |
Correct |
53 ms |
664 KB |
Output is correct |
21 |
Correct |
53 ms |
696 KB |
Output is correct |
22 |
Correct |
25 ms |
656 KB |
Output is correct |
23 |
Correct |
17 ms |
628 KB |
Output is correct |
24 |
Correct |
51 ms |
704 KB |
Output is correct |
25 |
Correct |
30 ms |
628 KB |
Output is correct |
26 |
Correct |
30 ms |
716 KB |
Output is correct |
27 |
Correct |
31 ms |
716 KB |
Output is correct |
28 |
Correct |
29 ms |
692 KB |
Output is correct |
29 |
Correct |
33 ms |
704 KB |
Output is correct |
30 |
Correct |
34 ms |
728 KB |
Output is correct |
31 |
Correct |
31 ms |
636 KB |
Output is correct |
32 |
Correct |
34 ms |
656 KB |
Output is correct |
33 |
Correct |
30 ms |
716 KB |
Output is correct |
34 |
Correct |
31 ms |
688 KB |
Output is correct |
35 |
Correct |
27 ms |
3152 KB |
Output is correct |
36 |
Correct |
30 ms |
3204 KB |
Output is correct |
37 |
Correct |
181 ms |
3140 KB |
Output is correct |
38 |
Correct |
187 ms |
3240 KB |
Output is correct |
39 |
Correct |
45 ms |
3136 KB |
Output is correct |
40 |
Correct |
176 ms |
3100 KB |
Output is correct |
41 |
Correct |
167 ms |
3188 KB |
Output is correct |
42 |
Correct |
173 ms |
3152 KB |
Output is correct |
43 |
Correct |
189 ms |
3108 KB |
Output is correct |
44 |
Correct |
158 ms |
3028 KB |
Output is correct |
45 |
Correct |
160 ms |
3184 KB |
Output is correct |
46 |
Correct |
156 ms |
3040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
852 KB |
Output is correct |
2 |
Correct |
5 ms |
704 KB |
Output is correct |
3 |
Correct |
5 ms |
696 KB |
Output is correct |
4 |
Correct |
8 ms |
724 KB |
Output is correct |
5 |
Correct |
21 ms |
864 KB |
Output is correct |
6 |
Correct |
54 ms |
19788 KB |
Output is correct |
7 |
Correct |
60 ms |
23420 KB |
Output is correct |
8 |
Correct |
60 ms |
24792 KB |
Output is correct |
9 |
Correct |
64 ms |
24836 KB |
Output is correct |
10 |
Correct |
9 ms |
744 KB |
Output is correct |
11 |
Correct |
15 ms |
724 KB |
Output is correct |
12 |
Correct |
54 ms |
708 KB |
Output is correct |
13 |
Correct |
52 ms |
644 KB |
Output is correct |
14 |
Correct |
28 ms |
748 KB |
Output is correct |
15 |
Correct |
18 ms |
724 KB |
Output is correct |
16 |
Correct |
53 ms |
680 KB |
Output is correct |
17 |
Correct |
36 ms |
716 KB |
Output is correct |
18 |
Correct |
33 ms |
700 KB |
Output is correct |
19 |
Correct |
31 ms |
720 KB |
Output is correct |
20 |
Correct |
28 ms |
712 KB |
Output is correct |
21 |
Correct |
30 ms |
628 KB |
Output is correct |
22 |
Correct |
32 ms |
696 KB |
Output is correct |
23 |
Correct |
37 ms |
648 KB |
Output is correct |
24 |
Correct |
36 ms |
732 KB |
Output is correct |
25 |
Correct |
36 ms |
716 KB |
Output is correct |
26 |
Correct |
32 ms |
716 KB |
Output is correct |
27 |
Correct |
9 ms |
724 KB |
Output is correct |
28 |
Correct |
15 ms |
644 KB |
Output is correct |
29 |
Correct |
53 ms |
664 KB |
Output is correct |
30 |
Correct |
53 ms |
696 KB |
Output is correct |
31 |
Correct |
25 ms |
656 KB |
Output is correct |
32 |
Correct |
17 ms |
628 KB |
Output is correct |
33 |
Correct |
51 ms |
704 KB |
Output is correct |
34 |
Correct |
30 ms |
628 KB |
Output is correct |
35 |
Correct |
30 ms |
716 KB |
Output is correct |
36 |
Correct |
31 ms |
716 KB |
Output is correct |
37 |
Correct |
29 ms |
692 KB |
Output is correct |
38 |
Correct |
33 ms |
704 KB |
Output is correct |
39 |
Correct |
34 ms |
728 KB |
Output is correct |
40 |
Correct |
31 ms |
636 KB |
Output is correct |
41 |
Correct |
34 ms |
656 KB |
Output is correct |
42 |
Correct |
30 ms |
716 KB |
Output is correct |
43 |
Correct |
31 ms |
688 KB |
Output is correct |
44 |
Correct |
27 ms |
3152 KB |
Output is correct |
45 |
Correct |
30 ms |
3204 KB |
Output is correct |
46 |
Correct |
181 ms |
3140 KB |
Output is correct |
47 |
Correct |
187 ms |
3240 KB |
Output is correct |
48 |
Correct |
45 ms |
3136 KB |
Output is correct |
49 |
Correct |
176 ms |
3100 KB |
Output is correct |
50 |
Correct |
167 ms |
3188 KB |
Output is correct |
51 |
Correct |
173 ms |
3152 KB |
Output is correct |
52 |
Correct |
189 ms |
3108 KB |
Output is correct |
53 |
Correct |
158 ms |
3028 KB |
Output is correct |
54 |
Correct |
160 ms |
3184 KB |
Output is correct |
55 |
Correct |
156 ms |
3040 KB |
Output is correct |
56 |
Correct |
23 ms |
740 KB |
Output is correct |
57 |
Correct |
37 ms |
724 KB |
Output is correct |
58 |
Correct |
26 ms |
912 KB |
Output is correct |
59 |
Correct |
5 ms |
724 KB |
Output is correct |
60 |
Correct |
5 ms |
724 KB |
Output is correct |
61 |
Correct |
8 ms |
712 KB |
Output is correct |
62 |
Correct |
21 ms |
980 KB |
Output is correct |
63 |
Correct |
55 ms |
19876 KB |
Output is correct |
64 |
Correct |
69 ms |
23432 KB |
Output is correct |
65 |
Correct |
58 ms |
24864 KB |
Output is correct |
66 |
Correct |
62 ms |
24792 KB |
Output is correct |
67 |
Correct |
10 ms |
684 KB |
Output is correct |
68 |
Correct |
19 ms |
660 KB |
Output is correct |
69 |
Correct |
52 ms |
704 KB |
Output is correct |
70 |
Correct |
49 ms |
688 KB |
Output is correct |
71 |
Correct |
25 ms |
724 KB |
Output is correct |
72 |
Correct |
18 ms |
724 KB |
Output is correct |
73 |
Correct |
57 ms |
740 KB |
Output is correct |
74 |
Correct |
33 ms |
708 KB |
Output is correct |
75 |
Correct |
36 ms |
716 KB |
Output is correct |
76 |
Correct |
35 ms |
800 KB |
Output is correct |
77 |
Correct |
30 ms |
692 KB |
Output is correct |
78 |
Correct |
31 ms |
668 KB |
Output is correct |
79 |
Correct |
35 ms |
844 KB |
Output is correct |
80 |
Correct |
31 ms |
712 KB |
Output is correct |
81 |
Correct |
29 ms |
724 KB |
Output is correct |
82 |
Correct |
31 ms |
716 KB |
Output is correct |
83 |
Correct |
30 ms |
700 KB |
Output is correct |
84 |
Correct |
30 ms |
3152 KB |
Output is correct |
85 |
Correct |
36 ms |
3188 KB |
Output is correct |
86 |
Correct |
180 ms |
3276 KB |
Output is correct |
87 |
Correct |
155 ms |
3124 KB |
Output is correct |
88 |
Correct |
43 ms |
3184 KB |
Output is correct |
89 |
Correct |
170 ms |
3060 KB |
Output is correct |
90 |
Correct |
165 ms |
3092 KB |
Output is correct |
91 |
Correct |
160 ms |
3108 KB |
Output is correct |
92 |
Correct |
167 ms |
3124 KB |
Output is correct |
93 |
Correct |
210 ms |
3096 KB |
Output is correct |
94 |
Correct |
157 ms |
3036 KB |
Output is correct |
95 |
Correct |
157 ms |
3020 KB |
Output is correct |
96 |
Correct |
464 ms |
24808 KB |
Output is correct |
97 |
Correct |
56 ms |
700 KB |
Output is correct |
98 |
Correct |
413 ms |
24768 KB |
Output is correct |
99 |
Correct |
92 ms |
24896 KB |
Output is correct |
100 |
Correct |
35 ms |
692 KB |
Output is correct |
101 |
Correct |
394 ms |
24808 KB |
Output is correct |
102 |
Correct |
386 ms |
24864 KB |
Output is correct |
103 |
Correct |
407 ms |
24864 KB |
Output is correct |
104 |
Correct |
404 ms |
24864 KB |
Output is correct |
105 |
Correct |
402 ms |
24820 KB |
Output is correct |
106 |
Correct |
362 ms |
24832 KB |
Output is correct |
107 |
Correct |
369 ms |
24852 KB |
Output is correct |
108 |
Correct |
347 ms |
24908 KB |
Output is correct |
109 |
Correct |
402 ms |
24804 KB |
Output is correct |