#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define chmax(x, v) x = max(x, v)
#define chmin(x, v) x = min(x, v)
#define pb push_back
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define x first
#define y second
#define int long long
using namespace std;
const int N = 1e5 + 5, G = 11, B = (1 << G);
const int OO = 1e18;
int nAvant[N][G], nApres[N][G];
vector<int> occs[G];
int dp[B];
int nInversions(int n){
return (n * n - n);
}
signed main(){
string line; cin >> line;
int nVals = line.length();
vector<int> vals;
for (int i = 0; i < nVals; ++i)
vals.pb(line[i] - 'A');
for (int c = 0; c < G; ++c)
nAvant[0][c] = 0;
nAvant[0][vals[0]]++;
for (int iVal = 1; iVal < nVals; ++iVal){
for (int val = 0; val < G; ++val)
nAvant[iVal][val] = nAvant[iVal - 1][val];
nAvant[iVal][vals[iVal]]++;
}
for (int iVal = 0; iVal < nVals; ++iVal)
for (int val = 0; val < G; ++val)
nApres[iVal][val] = nAvant[nVals - 1][val] - nAvant[iVal][val];
/*
cout << "wsh" << endl;
for (int i = 0; i < nVals; ++i)
cout << nAvant[i][2] << " ";
cout << endl;
for (int i = 0; i < nVals; ++i)
cout << nApres[i][2] << " ";
cout << endl;
cout << "-----" << endl;
*/
for (int iVal = 0; iVal < nVals; ++iVal)
occs[vals[iVal]].pb(iVal);
for (int mask = 0; mask < B; ++mask)
dp[mask] = OO;
dp[0] = 0;
for (int mask = 0; mask < B; ++mask){
vector<int> presents;
for (int i = 0; i < G; ++i){
if ((mask&(1 << i)) > 0)
presents.pb(i);
}
// cout << dp[mask] << endl;
for (int c = 0; c < G; ++c){
if ((mask&(1 << c)) == 0){
int nCur = sz(occs[c]);
if (nCur == 0){
chmin(dp[mask|(1 << c)], dp[mask]);
continue;
}
vector<int> before, after;
for (int iVal : occs[c]){
int sum = 0;
for (int present : presents)
sum += nAvant[iVal][present];
before.pb(sum);
sum = 0;
for (int present : presents)
sum += nApres[iVal][present];
after.pb(sum);
}
vector<int> prefix(nCur, 0), suffix(nCur, 0);
prefix[0] = before[0];
for (int i = 1; i < nCur; ++i)
prefix[i] = prefix[i - 1] + before[i];
suffix[nCur - 1] = after[nCur - 1];
for (int i = nCur - 2; i >= 0; --i)
suffix[i] = suffix[i + 1] + after[i];
int best = OO;
for (int coupe = -1; coupe < nCur; ++coupe){
int actu = 0;
if (coupe >= 0)
actu += 4 * prefix[coupe];
if (coupe + 1 < nCur)
actu += 4 * suffix[coupe + 1];
actu += nInversions(coupe + 1);
actu += nInversions(nCur - coupe - 1);
chmin(best, actu);
}
if (false && mask == 4){
cout << "trans " << c << " " << best << endl;
for (int iVal : occs[c])
cout << iVal << " ";
cout << endl;
for (int i : before)
cout << i << " ";
cout << endl;
for (int i : after)
cout << i << " ";
cout << endl;
cout << "---" << endl;
}
chmin(dp[mask|(1 << c)], dp[mask] + best);
}
}
}
cout << dp[B - 1]/4.0 << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
264 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
352 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
3 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
212 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
264 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
352 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
3 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
212 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
3 ms |
340 KB |
Output is correct |
20 |
Correct |
5 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
3 ms |
212 KB |
Output is correct |
27 |
Correct |
4 ms |
340 KB |
Output is correct |
28 |
Correct |
3 ms |
340 KB |
Output is correct |
29 |
Correct |
3 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
212 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
336 KB |
Output is correct |
33 |
Correct |
3 ms |
340 KB |
Output is correct |
34 |
Correct |
3 ms |
340 KB |
Output is correct |
35 |
Incorrect |
247 ms |
2580 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |