# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1101210 | andrei_iorgulescu | Boarding Passes (BOI22_passes) | C++14 | 3 ms | 23004 KiB |
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>
using namespace std;
#define int long long
using ld = long double;
ld inf = 1e12;
int n, g;
char a[100005];
int v[100005];
string ssss;
vector<int> pos[20];
int p[20][20][100005], s[20][20][100005];
int sp[100005];
ld dp[50005];
ld f(int b, int mask, int x)
{
ld ans = dp[mask - (1 << b)];
ld xxx = x;
ans += ((xxx * (xxx - 1)) / 4.0d);
ld xx = pos[b].size() - x;
ans += ((xx * (xx - 1)) / 4.0d);
//cout << b << ' ' << mask << ' ' << x << ' ';
for (int b1 = 0; b1 < g; b1++)
{
if (b1 != b and ((1 << b1) & mask))
{
ans += p[b][b1][x];
ans += s[b][b1][pos[b].size() - x];
}
}
//cout << ans << endl;
return ans;
}
signed main()
{
cin >> ssss;
n = ssss.size();
for (int i = 1; i <= n; i++)
a[i] = ssss[i - 1];
for (int i = 1; i <= n; i++)
v[i] = a[i] - 'A', g = max(g, v[i] + 1);
for (int i = 1; i <= n; i++)
pos[v[i]].push_back(i);
for (int i = 0; i < g; i++)
{
for (int j = 0; j < g; j++)
{
if (i == j)
continue;
for (int q = 1; q <= n; q++)
{
sp[q] = sp[q - 1];
if (v[q] == j)
sp[q]++;
}
for (int q = 0; q <= pos[i].size(); q++)
{
int sm = 0;
if (q > 1)
sm = p[i][j][q - 1];
if (q != 0)
sm += sp[pos[i][q - 1]];
p[i][j][q] = sm;
}
for (int q = 0; q <= pos[i].size(); q++)
{
int sm = 0;
if (q > 1)
sm = s[i][j][q - 1];
if (q != 0)
sm += (sp[n] - sp[pos[i][pos[i].size() - q]]);
s[i][j][q] = sm;
}
}
}
for (int mask = 1; mask < (1 << g); mask++)
{
dp[mask] = inf;
for (int b = 0; b < g; b++)
{
if (mask & (1 << b))
{
int st = 0, pas = 1 << 16;
while (pas != 0)
{
if (st + pas + 1 <= pos[b].size() and f(b, mask, st + pas) <= f(b, mask, st + pas + 1))
st += pas;
pas /= 2;
}
for (int dlt = -2; dlt <= 2; dlt++)
if (st + dlt >= 0 and st + dlt <= pos[b].size())
dp[mask] = min(dp[mask], f(b, mask, st + dlt));
}
}
}
cout << setprecision(5) << fixed;
cout << dp[(1 << g) - 1];
return 0;
}
Compilation message (stderr)
# | 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... |