Submission #253530

#TimeUsernameProblemLanguageResultExecution timeMemory
253530MarkoesPotatoes and fertilizers (LMIO19_bulves)C++14
0 / 100
3 ms2432 KiB
#include<bits/stdc++.h> using namespace std; int n,a,b[500005],c[500005],id,val; vector<pair<int,int> > va,slv; set<int> vb; int main(){ ios_base::sync_with_stdio(NULL); cin.tie(0);cout.tie(0); cin>>n; for(int i=1;i<=n;i++){ cin>>b[i]>>a; a -= min(a,b[i]); b[i] -= min(a,b[i]); if(a>0){ va.push_back(make_pair(i,a)); } if(b[i]>0){ vb.insert(i); } } memset(c,0,sizeof c); for(auto x : va){ id = x.first; val = x.second; //cout<<id<<"-id val-"<<val<<endl;; while(val>0){ auto kanan = vb.lower_bound(id); //cout<<*kanan<<endl; if(kanan == vb.begin()){ //cout<<"depan"<<endl; if(b[*kanan] <= val){ val -= b[*kanan]; vb.erase(kanan); //slv.push_back(make_pair(*kanan,b[*kanan])); c[*kanan] += b[*kanan]; } else{ b[*kanan] -= val; //slv.push_back(make_pair(*kanan,val)); c[*kanan] += val; val = 0; } continue; } if(kanan == vb.end()){ //cout<<"belakang"<<endl; kanan--; if(b[*kanan] <= val){ val -= b[*kanan]; vb.erase(kanan); //slv.push_back(make_pair(*kanan,b[*kanan])); c[*kanan] += b[*kanan]; } else{ b[*kanan] -= val; //slv.push_back(make_pair(*kanan,val)); c[*kanan] += val; val = 0; } continue; } auto kiri = kanan; kiri--; //cout<<*kiri<<' '<<*kanan<<endl; if(id - *kiri > *kanan - id){ if(b[*kanan] <= val){ val -= b[*kanan]; vb.erase(kanan); c[*kanan] += b[*kanan]; //slv.push_back(make_pair(*kanan,b[*kanan])); } else{ b[*kanan] -= val; //slv.push_back(make_pair(*kanan,val)); c[*kanan] += val; val = 0; } } else{ if(b[*kiri] <= val){ val -= b[*kiri]; vb.erase(kiri); //slv.push_back(make_pair(*kiri,b[*kiri])); c[*kiri] += b[*kiri]; } else{ b[*kiri] -= val; //slv.push_back(make_pair(*kiri,val)); c[*kiri] += val; val = 0; } } } } int v; long long ans=0; v = 1; for(auto x: va){ while(c[v]<=x.second && v<n){ ans += c[v] * 1ll * abs(v-x.first); x.second -= c[v]; v++; } ans += x.second * 1ll * abs(v-x.first); c[v] -= x.second; } cout<<ans<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...