#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fir first
#define sec second
#define pdd pair<long double, long double>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ld long double
#define pmax pair<__int128, __int128>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef unsigned int uint;
using namespace std;
int dx[4]= {0, 1, 0, -1}, dy[4]= {1, 0, -1, 0};
int ddx[8]={1, 1, 0, -1, -1, -1, 0, 1}, ddy[8]={0, 1, 1, 1, 0, -1, -1, -1};
const int mxN=100010;
const int mxM=35000;
const int mxK=1000000000;
const int MOD=1e9+7;
const ll INF=2e18;
int N;
char S[mxN];
int pref[mxN][15];
ll cost[mxN][16][16];
vector <int> apos[16];
bool alph[mxN][16];
ll dp[mxM];
bool Chk[mxM];
ll sum(int s, int e, int idx)
{
return pref[e][idx]-pref[s-1][idx];
}
ll calc(int now, ll pos, vector <int> &comp)
{
ll val=0, nowpos=(pos==0 ? 0 : apos[now][pos-1]);
for(int nxt : comp) if(nxt!=now)
{
val+=cost[nowpos][now][nxt];
}
ll siz=apos[now].size();
val+=pos*(pos-1)/2+(siz-pos)*(siz-pos-1)/2;
return val;
}
void dping(int idx)
{
//printf("idx=%d\n", idx);
vector <int> comp;
for(int i=0;i<15;i++) if((1<<i)&idx)
{
comp.push_back(i);
if(!Chk[idx-(1<<i)]) dping(idx-(1<<i));
}
for(int ele : comp) if(apos[ele].empty())
{
dp[idx]=dp[idx-(1<<ele)];
Chk[idx]=true;
return;
}
dp[idx]=INF;
for(int now : comp)
{
int s=0, e=apos[now].size();
while(s<=e-3)
{
int mid1=(s+e)/2, mid2=(s+e)/2+1;
if(calc(now, mid1, comp)<calc(now, mid2, comp)) e=mid2;
else s=mid1;
}
for(int i=s;i<=e;i++)
{
dp[idx]=min(dp[idx], dp[idx-(1<<now)]+calc(now, i, comp));
}
}
Chk[idx]=true;
}
int main()
{
gibon
cin >> S+1;
N=strlen(S+1);
for(int i=0;i<15;i++)
{
for(int j=1;j<=N;j++) if(S[j]==i+'A')
{
alph[j][i]=1;
apos[i].push_back(j);
}
}
for(int i=0;i<15;i++) for(int j=1;j<=N;j++) pref[j][i]=pref[j-1][i]+alph[j][i];
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++) if(i!=j && apos[i].size() && apos[j].size())
{
for(int ele : apos[i]) cost[0][i][j]+=2*sum(ele, N, j);
int siz=apos[i].size();
for(int k=0;k<apos[i].size();k++)
{
int now=apos[i][k], pre=(k==0 ? 0 : apos[i][k-1]);
cost[now][i][j]=cost[pre][i][j]-2*sum(now, N, j)+2*sum(1, now, j);
//printf("cost[%d][%d][%d]=%lld\n", now, i, j, cost[now][i][j]);
}
}
}
Chk[0]=true;
for(int i=0;i<15;i++) Chk[1<<i]=true;
for(int i=0;i<15;i++)
{
ll nsiz=apos[i].size(), mid=nsiz/2;
dp[1<<i]=mid*(mid-1)/2+(nsiz-mid)*(nsiz-mid-1)/2;
}
dping((1<<15)-1);
if(dp[(1<<15)-1]%2==0) cout << dp[(1<<15)-1]/2;
else cout << dp[(1<<15)-1]/2 << ".5";
}
Compilation message
passes.cpp: In function 'int main()':
passes.cpp:81:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | cin >> S+1;
| ~^~
passes.cpp:98:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for(int k=0;k<apos[i].size();k++)
| ~^~~~~~~~~~~~~~~
passes.cpp:97:17: warning: unused variable 'siz' [-Wunused-variable]
97 | int siz=apos[i].size();
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
596 KB |
Output is correct |
2 |
Correct |
9 ms |
596 KB |
Output is correct |
3 |
Correct |
8 ms |
640 KB |
Output is correct |
4 |
Correct |
9 ms |
596 KB |
Output is correct |
5 |
Correct |
7 ms |
720 KB |
Output is correct |
6 |
Correct |
11 ms |
7028 KB |
Output is correct |
7 |
Correct |
11 ms |
8148 KB |
Output is correct |
8 |
Correct |
12 ms |
8696 KB |
Output is correct |
9 |
Correct |
14 ms |
8788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
596 KB |
Output is correct |
2 |
Correct |
6 ms |
596 KB |
Output is correct |
3 |
Correct |
7 ms |
844 KB |
Output is correct |
4 |
Correct |
7 ms |
724 KB |
Output is correct |
5 |
Correct |
7 ms |
724 KB |
Output is correct |
6 |
Correct |
7 ms |
596 KB |
Output is correct |
7 |
Correct |
7 ms |
808 KB |
Output is correct |
8 |
Correct |
7 ms |
700 KB |
Output is correct |
9 |
Correct |
6 ms |
596 KB |
Output is correct |
10 |
Correct |
6 ms |
596 KB |
Output is correct |
11 |
Correct |
6 ms |
596 KB |
Output is correct |
12 |
Correct |
7 ms |
696 KB |
Output is correct |
13 |
Correct |
8 ms |
692 KB |
Output is correct |
14 |
Correct |
10 ms |
700 KB |
Output is correct |
15 |
Correct |
10 ms |
596 KB |
Output is correct |
16 |
Correct |
7 ms |
596 KB |
Output is correct |
17 |
Correct |
9 ms |
696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
596 KB |
Output is correct |
2 |
Correct |
6 ms |
596 KB |
Output is correct |
3 |
Correct |
7 ms |
844 KB |
Output is correct |
4 |
Correct |
7 ms |
724 KB |
Output is correct |
5 |
Correct |
7 ms |
724 KB |
Output is correct |
6 |
Correct |
7 ms |
596 KB |
Output is correct |
7 |
Correct |
7 ms |
808 KB |
Output is correct |
8 |
Correct |
7 ms |
700 KB |
Output is correct |
9 |
Correct |
6 ms |
596 KB |
Output is correct |
10 |
Correct |
6 ms |
596 KB |
Output is correct |
11 |
Correct |
6 ms |
596 KB |
Output is correct |
12 |
Correct |
7 ms |
696 KB |
Output is correct |
13 |
Correct |
8 ms |
692 KB |
Output is correct |
14 |
Correct |
10 ms |
700 KB |
Output is correct |
15 |
Correct |
10 ms |
596 KB |
Output is correct |
16 |
Correct |
7 ms |
596 KB |
Output is correct |
17 |
Correct |
9 ms |
696 KB |
Output is correct |
18 |
Correct |
7 ms |
596 KB |
Output is correct |
19 |
Correct |
6 ms |
596 KB |
Output is correct |
20 |
Correct |
7 ms |
840 KB |
Output is correct |
21 |
Correct |
7 ms |
724 KB |
Output is correct |
22 |
Correct |
7 ms |
856 KB |
Output is correct |
23 |
Correct |
6 ms |
596 KB |
Output is correct |
24 |
Correct |
7 ms |
724 KB |
Output is correct |
25 |
Correct |
6 ms |
596 KB |
Output is correct |
26 |
Correct |
7 ms |
596 KB |
Output is correct |
27 |
Correct |
7 ms |
700 KB |
Output is correct |
28 |
Correct |
7 ms |
696 KB |
Output is correct |
29 |
Correct |
7 ms |
596 KB |
Output is correct |
30 |
Correct |
8 ms |
696 KB |
Output is correct |
31 |
Correct |
9 ms |
596 KB |
Output is correct |
32 |
Correct |
7 ms |
596 KB |
Output is correct |
33 |
Correct |
6 ms |
596 KB |
Output is correct |
34 |
Correct |
9 ms |
596 KB |
Output is correct |
35 |
Correct |
7 ms |
1492 KB |
Output is correct |
36 |
Correct |
8 ms |
1496 KB |
Output is correct |
37 |
Correct |
17 ms |
21448 KB |
Output is correct |
38 |
Correct |
16 ms |
21484 KB |
Output is correct |
39 |
Correct |
17 ms |
21460 KB |
Output is correct |
40 |
Correct |
19 ms |
21384 KB |
Output is correct |
41 |
Correct |
17 ms |
20948 KB |
Output is correct |
42 |
Correct |
18 ms |
21056 KB |
Output is correct |
43 |
Correct |
20 ms |
21048 KB |
Output is correct |
44 |
Correct |
21 ms |
20948 KB |
Output is correct |
45 |
Correct |
17 ms |
21076 KB |
Output is correct |
46 |
Correct |
19 ms |
21020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
596 KB |
Output is correct |
2 |
Correct |
9 ms |
596 KB |
Output is correct |
3 |
Correct |
8 ms |
640 KB |
Output is correct |
4 |
Correct |
9 ms |
596 KB |
Output is correct |
5 |
Correct |
7 ms |
720 KB |
Output is correct |
6 |
Correct |
11 ms |
7028 KB |
Output is correct |
7 |
Correct |
11 ms |
8148 KB |
Output is correct |
8 |
Correct |
12 ms |
8696 KB |
Output is correct |
9 |
Correct |
14 ms |
8788 KB |
Output is correct |
10 |
Correct |
7 ms |
596 KB |
Output is correct |
11 |
Correct |
6 ms |
596 KB |
Output is correct |
12 |
Correct |
7 ms |
844 KB |
Output is correct |
13 |
Correct |
7 ms |
724 KB |
Output is correct |
14 |
Correct |
7 ms |
724 KB |
Output is correct |
15 |
Correct |
7 ms |
596 KB |
Output is correct |
16 |
Correct |
7 ms |
808 KB |
Output is correct |
17 |
Correct |
7 ms |
700 KB |
Output is correct |
18 |
Correct |
6 ms |
596 KB |
Output is correct |
19 |
Correct |
6 ms |
596 KB |
Output is correct |
20 |
Correct |
6 ms |
596 KB |
Output is correct |
21 |
Correct |
7 ms |
696 KB |
Output is correct |
22 |
Correct |
8 ms |
692 KB |
Output is correct |
23 |
Correct |
10 ms |
700 KB |
Output is correct |
24 |
Correct |
10 ms |
596 KB |
Output is correct |
25 |
Correct |
7 ms |
596 KB |
Output is correct |
26 |
Correct |
9 ms |
696 KB |
Output is correct |
27 |
Correct |
7 ms |
596 KB |
Output is correct |
28 |
Correct |
6 ms |
596 KB |
Output is correct |
29 |
Correct |
7 ms |
840 KB |
Output is correct |
30 |
Correct |
7 ms |
724 KB |
Output is correct |
31 |
Correct |
7 ms |
856 KB |
Output is correct |
32 |
Correct |
6 ms |
596 KB |
Output is correct |
33 |
Correct |
7 ms |
724 KB |
Output is correct |
34 |
Correct |
6 ms |
596 KB |
Output is correct |
35 |
Correct |
7 ms |
596 KB |
Output is correct |
36 |
Correct |
7 ms |
700 KB |
Output is correct |
37 |
Correct |
7 ms |
696 KB |
Output is correct |
38 |
Correct |
7 ms |
596 KB |
Output is correct |
39 |
Correct |
8 ms |
696 KB |
Output is correct |
40 |
Correct |
9 ms |
596 KB |
Output is correct |
41 |
Correct |
7 ms |
596 KB |
Output is correct |
42 |
Correct |
6 ms |
596 KB |
Output is correct |
43 |
Correct |
9 ms |
596 KB |
Output is correct |
44 |
Correct |
7 ms |
1492 KB |
Output is correct |
45 |
Correct |
8 ms |
1496 KB |
Output is correct |
46 |
Correct |
17 ms |
21448 KB |
Output is correct |
47 |
Correct |
16 ms |
21484 KB |
Output is correct |
48 |
Correct |
17 ms |
21460 KB |
Output is correct |
49 |
Correct |
19 ms |
21384 KB |
Output is correct |
50 |
Correct |
17 ms |
20948 KB |
Output is correct |
51 |
Correct |
18 ms |
21056 KB |
Output is correct |
52 |
Correct |
20 ms |
21048 KB |
Output is correct |
53 |
Correct |
21 ms |
20948 KB |
Output is correct |
54 |
Correct |
17 ms |
21076 KB |
Output is correct |
55 |
Correct |
19 ms |
21020 KB |
Output is correct |
56 |
Correct |
7 ms |
664 KB |
Output is correct |
57 |
Correct |
19 ms |
716 KB |
Output is correct |
58 |
Correct |
7 ms |
712 KB |
Output is correct |
59 |
Correct |
7 ms |
640 KB |
Output is correct |
60 |
Correct |
7 ms |
596 KB |
Output is correct |
61 |
Correct |
6 ms |
596 KB |
Output is correct |
62 |
Correct |
7 ms |
596 KB |
Output is correct |
63 |
Correct |
11 ms |
7012 KB |
Output is correct |
64 |
Correct |
13 ms |
8148 KB |
Output is correct |
65 |
Correct |
19 ms |
8660 KB |
Output is correct |
66 |
Correct |
15 ms |
8676 KB |
Output is correct |
67 |
Correct |
7 ms |
596 KB |
Output is correct |
68 |
Correct |
7 ms |
596 KB |
Output is correct |
69 |
Correct |
7 ms |
844 KB |
Output is correct |
70 |
Correct |
7 ms |
724 KB |
Output is correct |
71 |
Correct |
7 ms |
852 KB |
Output is correct |
72 |
Correct |
7 ms |
596 KB |
Output is correct |
73 |
Correct |
7 ms |
812 KB |
Output is correct |
74 |
Correct |
6 ms |
596 KB |
Output is correct |
75 |
Correct |
7 ms |
692 KB |
Output is correct |
76 |
Correct |
7 ms |
692 KB |
Output is correct |
77 |
Correct |
7 ms |
596 KB |
Output is correct |
78 |
Correct |
7 ms |
700 KB |
Output is correct |
79 |
Correct |
7 ms |
700 KB |
Output is correct |
80 |
Correct |
6 ms |
596 KB |
Output is correct |
81 |
Correct |
6 ms |
596 KB |
Output is correct |
82 |
Correct |
7 ms |
596 KB |
Output is correct |
83 |
Correct |
7 ms |
596 KB |
Output is correct |
84 |
Correct |
7 ms |
1492 KB |
Output is correct |
85 |
Correct |
7 ms |
1492 KB |
Output is correct |
86 |
Correct |
23 ms |
21460 KB |
Output is correct |
87 |
Correct |
19 ms |
21460 KB |
Output is correct |
88 |
Correct |
21 ms |
21492 KB |
Output is correct |
89 |
Correct |
22 ms |
21452 KB |
Output is correct |
90 |
Correct |
20 ms |
21060 KB |
Output is correct |
91 |
Correct |
18 ms |
20940 KB |
Output is correct |
92 |
Correct |
16 ms |
20948 KB |
Output is correct |
93 |
Correct |
17 ms |
21036 KB |
Output is correct |
94 |
Correct |
19 ms |
21060 KB |
Output is correct |
95 |
Correct |
17 ms |
21064 KB |
Output is correct |
96 |
Correct |
244 ms |
209060 KB |
Output is correct |
97 |
Correct |
16 ms |
724 KB |
Output is correct |
98 |
Correct |
209 ms |
209032 KB |
Output is correct |
99 |
Correct |
125 ms |
209044 KB |
Output is correct |
100 |
Correct |
26 ms |
596 KB |
Output is correct |
101 |
Correct |
218 ms |
209032 KB |
Output is correct |
102 |
Correct |
239 ms |
209048 KB |
Output is correct |
103 |
Correct |
205 ms |
208076 KB |
Output is correct |
104 |
Correct |
204 ms |
208216 KB |
Output is correct |
105 |
Correct |
221 ms |
208204 KB |
Output is correct |
106 |
Correct |
215 ms |
208136 KB |
Output is correct |
107 |
Correct |
228 ms |
208132 KB |
Output is correct |
108 |
Correct |
194 ms |
207908 KB |
Output is correct |
109 |
Correct |
206 ms |
208136 KB |
Output is correct |