Submission #977860

#TimeUsernameProblemLanguageResultExecution timeMemory
977860sleepntsheepCake 3 (JOI19_cake3)C11
100 / 100
467 ms6740 KiB
#include<stdio.h> #include<time.h> #include<stdlib.h> #define N 200000 int n, M, v[N],c[N],o[N],v2[N],vm[N],fw[N]; long long fw2[N],z=-1e18; void add_(int p,int k1,long long k2) { for(;p<N;p|=p+1)fw[p]+=k1,fw2[p]+=k2; } long long search() { long long val2=0;int val=0,pos=0; for(int j=1<<19;j>>=1;) if(pos+j<=N&&val+fw[pos+j-1]<=M)pos+=j,val+=fw[pos-1],val2+=fw2[pos-1]; return val2; } int cl=0,cr=-1; void add(int i) { add_(vm[i],1,v[i]); } void del(int i) { add_(vm[i],-1,-v[i]); } void move(int l,int r) { while(cr<r)add(++cr); while(cl>l)add(--cl); while(cr>r)del(cr--); while(cl<l)del(cl++); } void map(int i) { int lb=-1,ub=n,x=v[i]; while(ub-lb>1) { int mid=lb+(ub-lb)/2; if(v2[mid]<x)lb=mid; else ub=mid; } vm[i]=n-ub-1; } void sort(int *a,int l,int r) { while(l<r) { int i=l,j=l,k=r,t,p=a[l+rand()%(r-l)]; while(j<k) { int cmp; if(a==v2)cmp=a[j]-p; else cmp=c[a[j]]-c[p]; if(cmp<0) t=a[i],a[i]=a[j],a[j]=t,++i,++j; else if(cmp==0) ++j; else t=a[--k],a[k]=a[j],a[j]=t; } sort(a,l,i); l=k; } } void dnc(int l,int r,int optl,int optr) { if(l>r) return; int m=l+(r-l)/2,opt=-1; long long res=-1e18,res2; for(int i=optl;i<=optr&&i+M-1<=m;++i) { move(i,m); res2=search()-2*(c[m]-c[i]); if(res2>res)res=res2,opt=i; } if(res>z)z=res; dnc(l,m-1,optl,opt); dnc(m+1,r,opt,optr); } int main() { srand(time(0)); scanf("%d%d",&n,&M); for(int i=0;i<n;++i)scanf("%d%d",v+i,c+i),o[i]=i; sort(o,0,n); for(int i=0;i<n;++i)v2[i]=v[o[i]],vm[i]=c[o[i]]; for(int i=0;i<n;++i)c[i]=vm[i],v[i]=v2[i],o[i]=0; sort(v2,0,n); for(int i=0;i<n;++i) { map(i); /* duplicate element break fenwick search */ vm[i]-=o[vm[i]]++; } dnc(M-1,n-1,0,n-1); printf("%lld",z); }

Compilation message (stderr)

cake3.c: In function 'main':
cake3.c:82:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |     scanf("%d%d",&n,&M);
      |     ^~~~~~~~~~~~~~~~~~~
cake3.c:83:25: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |     for(int i=0;i<n;++i)scanf("%d%d",v+i,c+i),o[i]=i;
      |                         ^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...