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>
#include "boxes.h"
using namespace std;
using ll = long long int;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
ll min (ll x, ll y){
return ((x < y) ? x : y);
}
ll max (ll x, ll y){
return ((x > y) ? x : y);
}
const ll INF = 1e18;
long long delivery(int n, int k, int l, int p[]) {
ll rem = k;
vi fwd, bwd;
fwd.assign(k,0);
bwd.assign(k,0);
ll fwd_ctr = 0, bwd_ctr = 0;
for (ll i = 0; n > i; i++){
if (p[i] <= (l-1)/2){
fwd[i%k] += 2*p[i];
fwd_ctr++;
}
}
for (ll i = 0; n > i; i++){
if (p[n-i-1] >= (l+2)/2){
bwd[i%k] += 2*(l+1-p[n-i-1]);
bwd_ctr++;
}
}
/*
for (auto i: fwd) cout << i << " ";
cout << endl;
for (auto i: bwd) cout << i << " ";
cout << endl;
*/
ll mid_ctr = n-fwd_ctr-bwd_ctr;
rem -= mid_ctr;
if (fwd_ctr == 0 && bwd_ctr == 0){
return n;
}else if (fwd_ctr == 0){
if (mid_ctr == 1)
return bwd[bwd_ctr%k]+l;
else
return bwd[(bwd_ctr+k-1)%k];
}else if (bwd_ctr == 0){
if (mid_ctr == 1)
return fwd[fwd_ctr%k]+l;
else
return fwd[(fwd_ctr+k-1)%k];
}else{
ll res;
if (mid_ctr == 0)
res = bwd[(bwd_ctr+k-1)%k] + fwd[(fwd_ctr+k-1)%k];
else
res = bwd[bwd_ctr%k] + fwd[(fwd_ctr+k-1)%k] + l;
for (ll i = 1; k > i; i++){
ll fwd_te = (fwd_ctr+k-1-i)%k;
ll bwd_te = (bwd_ctr+k-1-(rem-i))%k;
res = min(res,fwd[fwd_te]+bwd[bwd_te]+l);
}
return res;
}
}
/*
#include <stdio.h>
#include <stdlib.h>
static char _buffer[1024];
static int _currentChar = 0;
static int _charsNumber = 0;
static FILE *_inputFile, *_outputFile;
static inline int _read() {
if (_charsNumber < 0) {
exit(1);
}
if (!_charsNumber || _currentChar == _charsNumber) {
_charsNumber = (int)fread(_buffer, sizeof(_buffer[0]), sizeof(_buffer), _inputFile);
_currentChar = 0;
}
if (_charsNumber <= 0) {
return -1;
}
return _buffer[_currentChar++];
}
static inline int _readInt() {
int c, x, s;
c = _read();
while (c <= 32) c = _read();
x = 0;
s = 1;
if (c == '-') {
s = -1;
c = _read();
}
while (c > 32) {
x *= 10;
x += c - '0';
c = _read();
}
if (s < 0) x = -x;
return x;
}
int main() {
_inputFile = fopen("boxes.in", "rb");
_outputFile = fopen("boxes.out", "w");
int N, K, L, i;
N = _readInt();
K = _readInt();
L = _readInt();
int *p = (int*)malloc(sizeof(int) * (unsigned int)N);
for (i = 0; i < N; i++) {
p[i] = _readInt();
}
fprintf(_outputFile, "%lld\n", delivery(N, K, L, p));
cout << "result: " << delivery(N,K,L,p) << endl;
return 0;
}
*/
# | 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... |