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;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
#define first fi;
#define second se;
vll facts = {1ll, 1ll, 2ll, 6ll, 24ll, 120ll, 720ll, 5040ll, 40320ll, 362880ll, 3628800ll};
ll delivery(int intn, int intk, int intl, int pos_int[]) {
ll n = intn, k = intk, l = intl;
vll pos(n, 0ll);
for(ll i = 0; i < n; i++) {
pos[i] = pos_int[i];
}
ll ans = 0ll;
if(n <= 10) {
ans = numeric_limits<ll>::max();
ll cur_cost = 0ll, cur_pos = 0ll, diff = 0ll;
vll inds;
for(ll i = 0; i < n; i++) {
inds.push_back(i);
}
for(ll i = 0; i < facts[n]; i++) {
cur_cost = cur_pos = 0ll;
for(ll j = 0; j < n; j++) {
diff = abs(cur_pos - pos[inds[j]]);
cur_cost += min(diff, l - diff);
cur_pos = pos[inds[j]];
if((j % k) == k - 1) {
cur_cost += min(cur_pos, l - cur_pos);
cur_pos = 0ll;
}
}
if(cur_pos != 0ll) {
cur_cost += min(cur_pos, l - cur_pos);
}
ans = min(ans, cur_cost);
next_permutation(inds.begin(), inds.end());
}
} else if(k == 1) {
for(ll i = 0; i < n; i++) {
ans += min(pos[i], l - pos[i]) << 1ll;
}
} else if(k == n) {
sort(pos.begin(), pos.end());
ll max_left = 0ll;
ll max_right = l;
for(ll i = 0; i < n; i++) {
if(pos[i] > (l >> 1ll) || ((l & 1) == 0ll && pos[i] == (l >> 1ll))) max_right = min(max_right, pos[i]);
if(l - pos[i] > (l >> 1ll)) max_left = max(max_left, pos[i]);
}
ans = min(l, (l - max_right + max_left) << 1ll);
}
return ans;
}
#ifdef DEBUG
int main() {
cin.tie(nullptr);
cout.tie(nullptr);
ios_base::sync_with_stdio(false);
int n, k, l;
cin >> n >> k >> l;
vi positions(n, 0);
for(int& pos : positions) {
cin >> pos;
}
cout << delivery(n, k, l, &positions[0]) << endl;
}
#endif
# | 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... |