이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*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--;
vector<ll>L1[15][15];
vector<ll>R1[15][15];
for (int w = 0; w < G; w++)
{
for (int v = 0; v < G; v++)
{
if (v == w)
continue;
L1[w][v] = vector<ll>(p[v].size() + 2, 0);
R1[w][v] = vector<ll>(p[v].size() + 2, 0);
vector<ll>&L = L1[w][v];
vector<ll>&R = R1[w][v];
auto it = p[v].begin();
for (int j : p[w])
{
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];
}
}
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 best = 1e18;
for (ll l = 0; l <= (int)p[v].size(); l++)
{
ll sum = 0;
for (int w = 0; w < G; w++)
{
if ((msk & (1 << w)) != 0)
{
sum += L1[w][v][l];
sum += R1[w][v][(int)p[v].size() - l];
}
}
ll r = p[v].size() - l;
sum += l * (l - 1);
sum += r * (r - 1);
best = min(best, sum);
}
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... |