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;
#define flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll,ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define EPS 0.000001
struct greateri
{
template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};
ll ans,kam;
map<ll,ll>gg;
map<ll,ll>toz;
set<ll>posi;
ll dist(ll x,ll y){
return min(abs(x-y),toz[x]+toz[y]);
}
ll findclosest(ll x){
auto k=posi.lower_bound(x);
ll n1=-1;
ll n2=-1;
if(k!=posi.end())n1=(*k);
else{
auto d=posi.begin();
n1=(*d);
}
if(k!=posi.begin()){
auto f=k;
f--;
n2=(*f);
}
else{
auto l=posi.end();
l--;
n2=(*l);
}
if(n1!=-1 && n2==-1)return n1;
if(n1==-1 && n2!=-1)return n2;
if(dist(x,n1)>dist(x,n2))return n2;
return n1;
}
void solve(ll node,ll cap){
if(node==0){
ll f=findclosest(node);
ans+=dist(node,f);
solve(f,cap);
}
else{
while(gg[node]>0 && cap>0){
gg[node]--,cap--;
}
if(gg[node]==0){posi.erase(node);}
if(posi.size()!=0 && cap>0){
ll f=findclosest(node);
ans+=dist(node,f);
solve(f,cap);
}
else if(posi.size()==0){ans+=toz[node];return;}
else if(cap==0)ans+=toz[node],solve(0,kam);
}
return;
}
long long delivery(int N,int K,int L,int positions[])
{
kam=K;
for (ll i = 0; i < N; ++i)
{
gg[positions[i]]++;
toz[positions[i]]=min(positions[i],L-positions[i]);
posi.insert(positions[i]);
}
posi.erase(0);
gg.erase(0);
solve(0,K);
return ans;
}
//code the AC sol !
// BS/queue/map
# | 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... |