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
HEHEHEHIHILOL
*/
#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);
for (int msk = 0; msk < (1 << 15); msk++)
{
for (int v = 0; v < 15; v++)
{
if ((msk & (1 << v)) == 0)
{
int msk1 = msk ^ (1 << v);
if (p[v].empty())
dp[msk1] = dp[msk];
else
{
ll L[p[v].size() + 1];
ll R[p[v].size() + 1];
L[0] = 0;
R[0] = 0;
for (ll i = 1; i <= (int)p[v].size(); i++)
{
L[i] = 0;
R[i] = 0;
}
for (int j = 0; j < (int)s.size(); j++)
{
if (((1 << (s[j] - 'A'))&msk) != 0)
{
auto it = upper_bound(p[v].begin(), p[v].end(), j);
if (it != p[v].end())
L[it - p[v].begin() + 1] += 4;
if (it != p[v].begin())
R[p[v].end() - it + 1] += 4;
}
}
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] += i * (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] += 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);
}
}
}
}
cout << fixed << setprecision(5) << double(dp[(1 << 15) - 1]) / 4 << "\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... |