This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
ABABABAB
*/
#pragma GCC optimize ("O3")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll dp[1 << 15];
int main()
{
ios_base::sync_with_stdio(false);
string s;
cin >> s;
for (int i = 0; i < (1 << 15); i++)
dp[i] = 1e18;
dp[0] = 0;
vector<int>p[15];
for (int i = 0; i < (int)s.size(); i++)
p[s[i] - 'A'].push_back(i);
int G = 15;
while (p[G - 1].empty())
G--;
for (int msk = 0; msk < (1 << G); msk++)
{
for (int v = 0; v < G; v++)
{
if ((msk & (1 << v)) == 0)
{
int msk1 = msk ^ (1 << v);
ll L[p[v].size() + 2];
ll R[p[v].size() + 2];
for (ll i = 0; i <= (int)p[v].size(); i++)
{
L[i] = 0;
R[i] = 0;
}
auto it = p[v].begin();
for (int j = 0; j < (int)s.size(); j++)
{
if (((1 << (s[j] - 'A'))&msk) != 0)
{
while (it != p[v].end() && (*it) <= j)
it++;
//auto it = lower_bound(p[v].begin(), p[v].end(), j);
L[(it - p[v].begin()) + 1] += 4;
R[(p[v].end() - it) + 1] += 4;
//cout << ";" << (char)('A' + v) << " " << s[j] << " " << j << " " << (it - p[v].begin()) + 1 << " " << (p[v].end() - it) + 1 << endl;
}
}
for (ll i = 1; i <= (int)p[v].size(); i++)
L[i] += L[i - 1];
for (ll i = 1; i <= (int)p[v].size(); i++)
L[i] += L[i - 1];
for (ll i = 1; i <= (int)p[v].size(); i++)
R[i] += R[i - 1];
for (ll i = 1; i <= (int)p[v].size(); i++)
{
R[i] += R[i - 1];
//cout << (char)('A' + v) << " " << i << " " << L[i] / 4 << " " << R[i] / 4 << endl;
}
for (ll i = 1; i <= (int)p[v].size(); i++)
L[i] += i * (i - 1);
for (ll i = 1; i <= (int)p[v].size(); i++)
R[i] += i * (i - 1);
ll best = 1e18;
for (int l = 0; l <= (int)p[v].size(); l++)
{
best = min(best, L[l] + R[(int)p[v].size() - l]);
}
dp[msk1] = min(dp[msk1], dp[msk] + best);
}
}
}
ll ret = dp[(1 << G) - 1];
cout << ret / 4;
if (ret % 4 == 2)
{
cout << ".5";
}
else if (ret % 4 != 0)
{
cout << "." << 25 * (ret % 4);
}
cout << "\n";
}
# | 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... |