小唯快跑啊
//这是我提交的代码,仅供参考 #include <stdio.h>#include <stdlib.h>#include <math.h> struct Node{ int X; int Y; int Y_tmp; int l; int r; int capacity;}node[100001]; int N,R; int cmp(struct Node *a, struct Node *b){ return a->r-b->r;} int Is_Sucess(int middle){ int i,j,start=0; for(i=0; i<N; i++) { node[i].capacity=middle; j=start; while(node[i].capacity>0 && node[j].l<=node[i].X && j<N) {/* if(node[j].Y_tmp==0) { j++; continue; }*/ if(node[j].r<node[i].X && node[j].Y_tmp>0) return 0; if(node[j].l<=node[i].X && node[j].r>=node[i].X) { if(node[i].capacity>=node[j].Y_tmp) { node[i].capacity-=node[j].Y_tmp; node[j].Y_tmp=0; start=j+1; } else { node[j].Y_tmp-=node[i].capacity; node[i].capacity=0; } } j++; } } if(node[N-1].Y_tmp>0) return 0; return 1;} int Binary_Search(int high, int low){ int i,middle; while(high!=low) { middle=(high+low)/2; for(i=0; i<N; i++) node[i].Y_tmp=node[i].Y; if(Is_Sucess(middle)) high=middle; else low=middle+1; } return high;} int main(){ int T; int i,j; int high; long long int low; scanf("%d", &T); while(T--) { scanf("%d%d", &N, &R); low=0; high=0; for(i=0; i<N; i++) { scanf("%d%d", &node[i].X, &node[i].Y); node[i].l=node[i].X-R; node[i].r=node[i].X+R; if(high<node[i].Y) high=node[i].Y; low+=node[i].Y; } qsort(node,N,sizeof(node[0]),cmp); low=low/N; high=Binary_Search(high,low); printf("%d\n",high); } system("pause"); return 0;}