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 "boxes.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <climits>
#include <cstring>
#include <functional>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using ii = pair<int,int>;
using vii = vector<pair<int,int>>;
using vb = vector<bool>;
template<typename T>
using Graph = vector<vector<T>>;
ll delivery(int n, int K, int L, int p[])
{
ll ans = 0;
if(K == 1)
{
for(int i = 0; i < n; ++i)
{
ans += 2LL * min(p[i], L - p[i]);
}
} else if(K == n)
{
ans = min(1LL * L, 2LL * p[n - 1]);
ans = min(ans, 2LL * (L - p[0]));
for(int i = 0; i < n - 1; ++i)
{
ans = min(ans, 2LL * p[i] + 2LL * (L - p[i + 1]));
}
} else if(n <= 10)
{
Graph<vector<ll>> dp(1 << n, Graph<ll>(n + 1, vector<ll>(K + 1, -1)));
function<ll(int,int,int)> run;
run = [&run, n, K, L, p, &dp](int mask, int i, int k) -> ll
{
// cerr << mask << ' ' << i << ' ' << k << '\n';
ll pos = 0;
if(i != n) pos = p[i];
if(mask == 0)
{
return min(pos, L - pos);
}
ll& res = dp[mask][i][k];
if(res != -1) return res;
res = LONG_MAX;
if(k == 0)
{
return res = min(pos, L - pos) + run(mask, n, K);
}
int tm = mask;
while(tm)
{
int bit = tm & -tm;
int j = __builtin_ctz(bit);
ll d = abs(p[j] - pos);
d = min(d, (ll)L - d);
res = min(res, run(mask - bit, j, k - 1) + d);
tm -= bit;
}
return res;
};
int mask = (1 << n) - 1;
ans = run(mask, n, K);
}
return ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |