This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define o 0
#define push_front push_back
int INF = 1LL<<62;
void solve() {
string S; cin >> S;
int n = S.size();
vector<int> ar(n); for (int i=0; i<n; i++) ar[i] = (S[i]-'A');
int G = *max_element(ar.begin(), ar.end()); G++;
int pref[n+1][G]; //count of character c, before index i
int suff[n+1][G]; //count of character c, after or equal to index i
for (int i=0; i<=n; i++) {
for (int j=0; j<G; j++) {
pref[i][j] = suff[i][j] = 0;
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<G; j++) {
if (j==ar[i]) {
pref[i+1][j] = pref[i][j]+1;
}
else {
pref[i+1][j] = pref[i][j];
}
}
}
for (int i=n-1; i>=0; i--) {
for (int j=0; j<G; j++) {
if (j==ar[i]) {
suff[i][j] = suff[i+1][j]+1;
}
else {
suff[i][j] = suff[i+1][j];
}
}
}
//everyone before L boards from the front, everyone after or equal to it boards from the back.
vector<vector<int>> counts(G,vector<int>(G,0)); //counts[i][j] = sum of subsequences of (i,j) before l, and subsequences of (j,i) after L
//counts[i][j] = sum of pref[k][j] for k<=L and ar[k]=i, and analogous expression for the suffix
//first, get initial count values (for everyone boarding from the back)
//and assuming that these are already boarded since the back breaker may not be the same for each group - that was false assumption
//so precomp counts then ternary search on the breakpoint
for (int j=0; j<G; j++) {
for (int k=0; k<n; k++) {
int i = ar[k];
//cout << "adding " << i <<" " << j <<" " << k <<" " << j << " " << suff[k][j] << endl;
counts[i][j] += suff[k+1][j];
}
}
vector<vector<vector<int>>> acounts;
acounts.push_back(counts);
for (int L=0; L<n; L++) {
//counts[i][j] = number of swaps required if i goes after j
//transfer L to the right
int cur = ar[L];
for (int j=0; j<G; j++) {
int delta = suff[L+1][j];
int delta2 = pref[L][j];
counts[cur][j] -= delta-delta2;
}
acounts.push_back(counts);
/*cout << "TRANSFERRED TO " << L+1 << endl;
for (int i=0; i<G; i++) {
for (int j=0; j<G; j++) {
cout << i <<" " << j <<" " << counts[i][j] << endl;
}
}
cout << endl;*/
}
//acounts[breakpoint][group_1][group_2]
//ABCABCAA
vector<int> dp;
vector<vector<int>> indices(G);
for (int i=0; i<n; i++) {
indices[ar[i]].push_back(i);
}
for (int i=0; i<G; i++) {
if (indices[i].size()==0) continue;
if (indices[i].front()!=0) indices[i].push_front(0);
if (indices[i].back()!=n-1) {
indices[i].push_back(n-1);
}
}
for (int i=0; i<G; i++) {
sort(indices[i].begin(), indices[i].end());
}
dp=vector<int>(1<<G,INF); //dp[mask] = min cost if this mask is at the beginning of the ordering
dp[0] = 0;
for (int mask=0; mask<1<<G; mask++) {
for (int addon=0; addon<G; addon++) {
if (mask&(1<<addon)) continue;
if (indices[addon].size()==0) {
dp[mask | (1<<addon)] = min(dp[mask | (1<<addon)],dp[mask]+o);
continue;
}
int l = 0; int r = indices[addon].size(); r--;
int mi = 1LL<<62;
while (l<r-1) {
//cout << l << " " << r << endl;
//cout << indices[addon][l] <<" " << indices[addon][r] << endl;
int m1,m2;
if (r-l>10) {
m1 = l+(r-l)/9;
m2 = r-(r-l)/9;
}
else {
m1 = l;
m2 = r;
}
m1=l; m2=r;
int am1 = 0;
for (int j=0; j<G; j++) {
if (mask&(1<<j)) {
am1+=2*acounts[indices[addon][m1]][addon][j];
}
if (j==addon) {
am1+=acounts[indices[addon][m1]][addon][j];
}
}
int am2 = 0;
for (int j=0; j<G; j++) {
if (mask&(1<<j)) {
am2+=2*acounts[indices[addon][m2]][addon][j];
}
if (j==addon) {
am2+=acounts[indices[addon][m2]][addon][j];
}
}
mi = min(mi,min(am1,am2));
//cout << "adding " << mask <<" " << addon <<" " << l <<" " << r <<" " << m1 <<" " << m2 <<" " << am1 <<" " << am2 << endl;
if (am1>am2) {
l=m1+1; r=r;
}
else if (am1<am2) {
l=l; r=m2-1;
}
else {
l=m1+1; r=m2;
}
//cout << mask <<" " << addon <<" " << al <<endl;
}
int al = 0;
for (int j=0; j<G; j++) {
if (mask&(1<<j)) {
al+=2*acounts[indices[addon][l]][addon][j];
}
if (j==addon) {
al+=acounts[indices[addon][l]][addon][j];
}
}
dp[mask | (1<<addon)] = min(dp[mask | (1<<addon)],dp[mask]+al);
if (l+1==indices[addon].size()) {
continue;
}
al = 0;
for (int j=0; j<G; j++) {
if (mask&(1<<j)) {
al+=2*acounts[indices[addon][l+1]][addon][j];
}
if (j==addon) {
al+=acounts[indices[addon][l+1]][addon][j];
}
}
dp[mask | (1<<addon)] = min(dp[mask | (1<<addon)],dp[mask]+al);
}
}
int ans=dp[(1<<G)-1];
int a2 = ans/2;
cout << a2;
if (ans%2==1) cout << ".50";
cout << endl;
}
signed main() {
int local=0;
if (local) {
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
}
/*#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#endif*/ //fast IO
solve();
}
Compilation message (stderr)
passes.cpp: In function 'void solve()':
passes.cpp:163:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
163 | if (l+1==indices[addon].size()) {
| ~~~^~~~~~~~~~~~~~~~~~~~~~~
passes.cpp: In function 'int main()':
passes.cpp:190:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
190 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
passes.cpp:192:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
192 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |