# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
140004 | babo | Boxes with souvenirs (IOI15_boxes) | C++14 | 0 ms | 0 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 "boxes.h"
#include <bits/stdc++.h>
using namespace std;
static long long sum1[10000010];
static long long sum2[10000010];
static long long gonext[10000010];
long long delivery(int N, int K, int L, int p[]) {
long long i;
long long ans=1000000000000000000ll;
for(i=0;i<N;i++)
{
gonext[i]=i;
sum1[i]=p[i]*2;
if(i>=K)
sum1[i]+=sum1[i-K];
}
for(i=N-1;i>=0;i--)
{
sum2[i]=(L-p[i])*2;
if(i+K<=N-1)
sum2[i]+=sum2[i+K];
}
for(i=0;i<N;i++)
{
if(p[i]>L/2) break;
}
int las=i-1;
for(i=0;i<=las;i++)
{
gonext[las-i]=las+1+((K-i-1)%K+K)%K;
}
/*for(i=0;i<N;i++)
printf("%lld ",gonext[i]);
puts("");*/
long long temp=L*gonext[0]/K;
if(gonext[0]<N)
temp+=sum2[gonext[0]];
ans=min(ans,temp);
i=-1;
//printf("%lld %lld %lld %lld %lld\n",i,0,L*(gonext[i+1]-(i+1)),gonext[i+1]<N?sum2[gonext[i+1]]:-1,temp);
for(i=0;i<N&&p[i]<=L/2;i++)
{
long long temp=sum1[i]+L*(gonext[i+1]-(i+1))/K;
if(gonext[i+1]<N)
temp+=sum2[gonext[i+1]];
//printf("%lld %lld %lld %lld %lld\n",i,sum1[i],L*(gonext[i+1]-(i+1)),gonext[i+1]<N?sum2[gonext[i+1]]:-1,temp);
ans=min(ans,temp);
}
return ans;
}
#include "boxes.h"
#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));
return 0;
}