# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
60811 | dukati8 | 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 <stdio.h>
#include <stdlib.h>
#include "boxes.h"
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a; i<int(b); i++)
#define all(x) x.begin(),x.end()
long long delivery(int N, int K, int L, int p[]) {
if (K==1) {
long long tot=0;
rep(i,0,N) {
tot+=2*min(p[i],L-p[i]);
}
return tot;
}
map<long long,int> places;
long long biggestplace=0;
rep(i,0,N) {
long long curr=p[i];
biggestplace=max(biggestplace,curr);
if (places.find(curr)!=places.begin()) places[curr]++;
else places[curr]=1;
}
map<int,long long> dpthing; //This dp should be, for every possible amount of boxes that chould have been transported
//to all the teams before and including the place we are at, minimum time to get there with those boxes
dpthing.insert(make_pair(0,0));
long long prevplace=0;
long long extratime=0;
int need=0;
for (auto a:places) {
long long loc=a.first;
int amount=a.second;
extratime+=(amount/K)*2*min(loc,L-loc);
amount%=K;
if (amount==0) continue;
long long mintime=1000000000000000;
need+=amount;
vector<int> rem;
for (auto b:dpthing) {
int have=b.first;
mintime=min(mintime,b.second);
if (have>=need) break;
rem.push_back(have);
}
for (auto b:rem) {
dpthing.erase(b);
}
//Now everything in dpthing has enough for this new location, they move there and increase time by the distance
extratime+=min(loc-prevplace,L-loc+prevplace);
//We can also go back and fetch new boxes, this should be added to the previous smallest mintime
mintime+=min(prevplace,L-prevplace) + min(loc,L-loc); //Time to go back and fetch more and go to new place
mintime-=min(loc-prevplace,L-loc+prevplace); //This was added to extratime instead
dpthing.insert(make_pair(need-amount+K,mintime)); //We would have need-amount+K after that
prevplace=loc;
}
//Now we take the least value in dpthing, add time to get back and extratime to it
long long best=1000000000000000;
for (auto a:dpthing) {
best=min(best,a.second);
}
return best + min(biggestplace,L-biggestplace) + extratime;
}
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;
}