# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
519615 | silverfish | Boxes with souvenirs (IOI15_boxes) | C++17 | 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>
#include "boxes.h"
using namespace std;
using ll = long long;
const ll N = 10000007, inf = 1e18+10;
struct node{
ll mn = 1e18, mnpos = -1;
void ins(ll v, int p){
if(v < mn){
mn = v;
mnpos = p;
}
}
};
ll dp[N], s[N];
node fen[N];
void ins(ll v, int p){
for(;p<N;p+=p&-p){
if(v < fen[p].mn){
fen[p].mn = v;
fen[p].mnpos = p;
}
}
}
int get(int p){
ll mn=inf;
int mnpos = -1;
for(;p>0;p-=p&-p){
if(fen[p].mn < mn){
mn = fen[p].mn;
mnpos = fen[p].mnpos;
}
}
return mnpos;
}
ll mn(ll a, ll b){
return (a < b ? a : b);
}
long long delivery(int n, int k, int L, int p[]) {
ll l = L;
fill(dp, dp+N, inf);
// sort(p, p+n);
for(int i = 0; i < n; ++i){
s[i] = (i ? dp[i-1] : 0) + mn(2LL*(l-p[i]), l);
ins(s[i], n-i);
int pos = get(n-i+k-1);
dp[i] = dp[pos-1]+mn(l, mn(2LL*p[i], 2LL*(l-p[pos])));
}
return dp[n-1];
}