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 inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 1e7+7;
int a[MAXN], nArr, k, L;
#ifdef Nhoksocqt1
ll delivery(int _N, int _K, int _L, int _pos[]);
int _P[MAXN];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
freopen("BOXES.inp", "r", stdin);
freopen("BOXES.out", "w", stdout);
int N, K, L;
cin >> N >> K >> L;
N = Random(1, 10), K = Random(1, N), L = Random(1, 1e9); cout << N << ' ' << K << ' ' << L << '\n';
for (int i = 0; i < N; ++i)
cin >> _P[i];
for (int i = 0; i < N; ++i) _P[i] = Random(0, L - 1); sort(_P, _P + N); for (int i = 0; i < N; ++i) cout << _P[i] << " \n"[i + 1 == N];
cout << delivery(N, K, L, _P) << '\n';
return 0;
}
#endif // Nhoksocqt1
ll delivery(int _N, int _K, int _L, int _pos[]) {
nArr = _N, k = _K, L = _L;
int cnt0(0);
while(cnt0 < nArr && _pos[cnt0] == 0)
++cnt0;
for (int i = cnt0 + 1; i <= nArr; ++i)
a[i - cnt0] = _pos[i - 1];
nArr = _N - cnt0;
ll result(0);
int l(1), r(nArr);
for (int i = 1; i <= nArr; ++i) {
if(2 * a[i] >= L)
break;
if(i % k == 0) {
result += 2 * a[i];
l = i + 1;
}
}
for (int i = nArr; i >= l; --i) {
if(2 * (L - a[i]) >= L)
break;
if((nArr - i + 1) % k == 0) {
result += 2 * (L - a[i]);
r = i - 1;
}
}
ll answer(1e18);
if(r - l >= 0) {
int cntL(0), cntR(0);
while(l + cntL <= r && 2 * a[l + cntL] < L)
++cntL;
while(r - cntR >= l && 2 * (L - a[r - cntR]) < L)
++cntR;
int cntM = r - l + 1 - cntL - cntR;
for (int dem = 0; dem <= cntL; ++dem) {
int costLeft = (dem > 0 ? min(L, 2 * a[l + dem - 1]) : 0);
int x = (cntM + cntL - dem + k - 1) / k;
int costRight = (dem + x * k <= r - l ? min(L, 2 * (L - a[l + dem + x * k])) : 0);
//cout << costLeft << ' ' << x * L << ' ' << costRight << '\n';
answer = min(answer, result + costLeft + 1LL * x * L + costRight);
}
} else {
answer = result;
}
ll answer2(1e18);
if(r - l >= 0) {
int last = min(k, r - l + 1);
for (int d1 = 0; d1 <= last; ++d1) {
for (int d2 = 0; d2 <= last && d1 + d2 <= r - l + 1; ++d2) {
int costLeft = (d1 > 0 ? min(L, 2 * a[l + d1 - 1]) : 0);
int costRight = (d2 > 0 ? min(L, 2 * (L - a[r - d2 + 1])) : 0);
ll costMid = (r - l + 1 - d1 - d2 + k - 1) / k * L;
//cout << d1 << ' ' << d2 << ' ' << costLeft << ' ' << costRight << ' ' << costMid << '\n';
answer2 = min(answer2, result + costLeft + costMid + costRight);
}
}
} else {
answer2 = result;
}
//cout << answer2 << " FIND: ";
return answer2;
}
# | 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... |