# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
238850 | lakshith_ | 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 <bits/stdc++.h>
#define ll long long
#define f first
#define s second
using namespace std;
inline void io_setup(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
inline ll Min(ll a,ll b){ return a<b?a:b; }
inline ll Max(ll a,ll b){ return a>b?a:b; }
ll delivery(int n,int k,int L,int positions[]){
ll ans;
if(k==1){
ans = 0;
for(int i=0;i<n;i++){
ans += 2*Min(positions[i],L-positions[i]);
}
}else if(k==n){
pair<int,int> l,r;
for(int i=0;i<n;i++){
if(positions[i]<(L/2)){
l.f = Min(positions[i],l.f);
l.s = Max(positions[i],l.s);
}else{
r.f = Min(L-positions[i],r.f);
r.s = Max(L-positions[i],r.s);
}
}
ans = min(L,2*r.s+2*l.s);
}else{
int l[n];
int r[n];
int g = k;
int pos = 0;
int prev = 0;
for(int i=0;i<n;i++){
l[i]=prev;
int dis = positions[i]-pos;
if(g==0){
l[i] += pos+positions[i];
prev = l[i];
l[i] += Min(positions[i],L-positions[i]);
g=k;
g--;
pos = positions[i];
}else{
l[i] += dis;
prev = l[i];
l[i] += Min(positions[i],L-positions[i]);
g--;
pos = positions[i];
}
pos = 0;
g = k;
prev=0;
for(int i=n-1;i>=0;i--){
positions[i] = L-positions[i];
r[i]=prev;
int dis = positions[i]-pos;
if(g==0){
r[i] += pos+positions[i];
prev = r[i];
r[i] += Min(positions[i],L-positions[i]);
g=k;
g--;
pos = positions[i];
}else{
r[i] += dis;
prev = r[i];
r[i] += Min(positions[i],L-positions[i]);
g--;
pos = positions[i];
}
}
}
// for(int i=0;i<n;i++)
// cout << l[i] << "\t";
// cout << "\n";
// for(int i=0;i<n;i++)
// cout << r[i] << "\t";
// cout << "\n";
// ll MIN = INT_MAX;
// for(int i=0;i<n-1;i++){
// MIN = Min(MIN,l[i]+r[i+1]);
// }
ans = MIN;
}
return ans;
}
// int main(){
// io_setup();
// int arr[] = {1,2,5};
// cout << delivery(3,2,8,arr);
// }