# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
933106 | sleepntsheep | Schools (IZhO13_school) | C++17 | 298 ms | 30116 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<stdio.h>
#include<utility>
#include<set>
int hi(int a,int b){return a>b?a:b;}
unsigned X=12345;int rand_(){return(X*=3)/2;}
int (*compar)(int,int);
void sort(int*aa,int l,int r){
while(l<r){int i=l,j=l,k=r,tmp,p=aa[l+rand_()%(r-l)];
while(j<k)switch(compar(aa[j],p)){case 0:++j;break;case -1:tmp=aa[j],aa[j]=aa[i],aa[i]=tmp,++i,++j;break;case 1:tmp=aa[--k],aa[k]=aa[j],aa[j]=tmp;break;}sort(aa,l,i);l=k;}}
#define N 500005
int n,m,s,o[N],a[N],b[N],f[N];long long z;
int bya(int i,int j){return a[i]<a[j]?1:a[i]>a[j]?-1:0;}
std::set<std::pair<int,int>>cm,cf,im,is,cf_nm;
int main(){
scanf("%d%d%d",&n,&m,&s);for(int i=0;i<n;++i)scanf("%d%d",a+i,b+i),o[i]=i;
compar=bya;sort(o,0,n);
for(int i=0;i<m;++i)cm.insert({b[o[i]]-a[o[i]],i}),z+=a[o[i]];
for(int i=m;i<n;++i)cf.insert({b[o[i]],i}), cf_nm.insert({a[o[i]],i});
for(int j=0;j<s;++j)
{
if(cm.empty()||(cf.size()&&cf.rbegin()->first>=cm.rbegin()->first+cf_nm.rbegin()->first))
{
int i=cf.rbegin()->second;
cf_nm.erase({a[o[i]],i});
cf.erase(std::prev(cf.end()));
z+=b[o[i]];
}
else
{
int i=cm.rbegin()->second;
cm.erase(std::prev(cm.end()));
int nm=cf_nm.rbegin()->second;
z+=b[o[i]]-a[o[i]]+a[o[nm]];
cm.insert({b[o[nm]]-a[o[nm]],nm});
cf.erase({b[o[nm]],nm});
cf_nm.erase(std::prev(cf_nm.end()));
}
}
for(auto[x,y]:im)z+=x;for(auto[x,y]:is)z+=x;
printf("%lld",z);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |