Submission #253545

#TimeUsernameProblemLanguageResultExecution timeMemory
253545MarkoesPotatoes and fertilizers (LMIO19_bulves)C++14
24 / 100
219 ms24792 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; if(a<b[i]){ b[i] -= a; a = 0; } else{ a -= b[i]; b[i] = 0; } if(a>0){ va.push_back(make_pair(i,a)); } if(b[i]>0){ vb.insert(i); } } long long aa=0; memset(c,0,sizeof c); for(auto x : va){ id = x.first; val = x.second; aa += val; //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; } } } } // long long cc=0;; // for(int i=1;i<=n;i++){ // cc += c[i]; // cout<<i<<' '<<c[i]<<endl; // } // cout<<endl; // assert(cc==aa); int v; long long ans=0; v = 1; for(auto x: va){ // cout<<x.first<<" id"<<endl; while(c[v]<=x.second && v<n){ // cout<<c[v]<<' '<<c[v] * 1ll * abs(v-x.first)<<endl; ans += c[v] * 1ll * abs(v-x.first); x.second -= c[v]; v++; } // cout<<x.second * 1ll * abs(v-x.first)<<endl; 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...