This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Bolatulu
#include <bits/stdc++.h>
// #include <art.h>
/*
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
*/
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define pb push_back
#define eb emplace_back
#define ins insert
#define F first
#define S second
#define md (tl+tr)/2
#define TL v+v,tl,md
#define TR v+v+1,md+1,tr
#define Tl t[v].l,tl,md
#define Tr t[v].r,md+1,tr
#define all(x) (x).begin(),(x).end()
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define int long long
// #define file(s) freopen(s".in", "r", stdin); freopen(s".out", "w", stdout);
using namespace std;
int binpow(int a,int n,int M) {
if (n==0)
return 1;
if (n%2!=0)
return (a * binpow(a,n-1,M))%M;
int z=binpow(a,n/2,M);
return (z*z)%M;
}
const ll INF = 1e18+7;
const int N = 1e5+7;
string s;
int n,dp[N],g,pref[16][16][N],suf[16][16][N],sz[16],L[N],R[N];
vector <int> d[N],bits[N];
int calc(int x,int mask,int b) {
int res=0;
for (auto i : bits[mask]) {
if (x>=0)
res+=pref[b][i][x];
if (x+1<sz[b])
res+=suf[b][i][x+1];
}
return res;
}
void solve() {
cin >> s;
n=s.size();
s=" "+s;
for (int i=1;i<=n;i++) {
g=max(g,(int)(s[i]-'A'+1));
d[s[i]-'A'].pb(i);
}
for (int i=0;i<g;i++)
sz[i]=d[i].size();
for (int i=0;i<g;i++) {
for (int j=0;j<g;j++) {
for (int k=0;k<sz[i];k++) {
int l=-1,r=sz[j];
while (l+1<r) {
int mid=(l+r)/2;
if (d[j][mid]<d[i][k])
l=mid;
else
r=mid;
}
if (i!=j)
L[k]=(l+1)*2, R[k]=(sz[j]-r)*2;
else
L[k]=k, R[k]=sz[i]-k-1;
if (k>0)
pref[i][j][k]=pref[i][j][k-1];
pref[i][j][k]+=L[k];
}
for (int k=sz[i]-1;k>=0;k--) {
if (k+1<sz[i])
suf[i][j][k]=suf[i][j][k+1];
suf[i][j][k]+=R[k];
}
}
}
for (int mask=1;mask<(1<<g);mask++) {
dp[mask]=INF;
for (int i=0;i<g;i++) {
if (mask>>i&1)
bits[mask].pb(i);
}
for (auto i : bits[mask]) {
int l=-1,r=sz[i]-1;
while (l+2<r) {
int m1=l+(r-l)/3, m2=r-(r-l)/3;
if (calc(m1,mask,i)<=calc(m2,mask,i))
r=m2;
else
l=m1;
}
int val=INF;
for (int j=l;j<=r;j++)
val=min(val,calc(j,mask,i));
dp[mask]=min(dp[mask],dp[mask^(1<<i)]+val);
}
}
cout << setprecision(1) << fixed << dp[(1<<g)-1]*0.5;
}
signed main() {
// file("jenga");
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int test = 1;
// cin >> test;
while (test--) {
solve();
if (test)
cout << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |