1 条题解

  • 0
    @ 2022-8-24 21:30:14
    #include<cstdio>
    #include<cstring>
    using namespace std;
    struct databox
    {
    	long long x;
    	int i;
    }x[600010],y[600010];
    inline void qsort1(int l,int r)
    {
    	register int i=l,j=r;register databox mid=x[(l+r)/2],t;
    	while(i<=j){
    		while(x[i].x<mid.x) i++;
    		while(x[j].x>mid.x) j--;
    		if(i<=j){
    			t=x[i];
    			x[i]=x[j];
    			x[j]=t;
    			i++;j--;
    		}
    	}
    	if(l<j) qsort1(l,j);
    	if(i<r) qsort1(i,r);
    }
    inline void qsort2(int l,int r)
    {
    	register int i=l,j=r;register databox mid=y[(l+r)/2],t;
    	while(i<=j){
    		while(y[i].x<mid.x) i++;
    		while(y[j].x>mid.x) j--;
    		if(i<=j){
    			t=y[i];
    			y[i]=y[j];
    			y[j]=t;
    			i++;j--;
    		}
    	}
    	if(l<j) qsort2(l,j);
    	if(i<r) qsort2(i,r);
    }
    int main()
    {
    	register int n;
    	register long long d;
    	register int i;
    	register long long nowans=0,ans=0,nxsum=0,nysum=0,nx,ny;
    	scanf("%d%lld",&n,&d);
    	for(i=1;i<=n;i++){
    		scanf("%lld%lld",&x[i].x,&y[i].x);
    		x[i].i=i,y[i].i=i;
    	}
    	qsort1(1,n);
    	qsort2(1,n);
    	for(i=1;i<=n;i++){
    		nxsum+=x[i].x;
    		nysum+=y[i].x;
    		nowans+=i*x[i].x-nxsum;
    		nowans+=i*y[i].x-nysum;
    	}
    	if(nowans<=d){
    		printf("-1\n");return 0;
    	}
    	register int l=1,r=n,mid;
    	while(l<r)
    	{
    		mid=(l+r)/2;nxsum=0,nysum=0;
    		nx=ny=0;nowans=0;
    		for(i=1;i<=n;i++){
    			if(x[i].i<=mid){
    				nx++;nxsum+=x[i].x;
    				nowans+=x[i].x*nx-nxsum;
    			}
    			if(y[i].i<=mid){
    				ny++;nysum+=y[i].x;
    				nowans+=y[i].x*ny-nysum;
    			}
    		}
    		if(nowans<=d) l=mid+1;
    		else r=mid;
    	}
    	printf("%d\n",l);
    	return 0;
    }
    
    • 1

    信息

    ID
    2715
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    38
    已通过
    7
    上传者