UVALive - 7261 Xiongnu's Land

思路:

  先二分下界,再二分上届。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define MP make_pair
 6 #define PB push_back
 7 typedef long long LL;
 8 typedef pair<int,int> PII;
 9 const double eps=1e-8;
10 const double pi=acos(-1.0);
11 const int K=1e6+7;
12 const int mod=1e9+7;
13 
14 struct node
15 {
16     int x,y,w,h;
17     node(){}
18     node(int a,int b,int c,int d){x=a,y=b,w=c,h=d;}
19 }rc[K];
20 
21 LL sum,ls;
22 LL check(int x,int n)
23 {
24     LL ret=0;
25     for(int i=1;i<=n;i++)
26     if(rc[i].x+rc[i].w<=x)
27         ret+=rc[i].w*1LL*rc[i].h;
28     else if(rc[i].x<=x)
29         ret+=1LL*(x-rc[i].x)*rc[i].h;
30     return ret;
31 }
32 int main(void)
33 {
34     //freopen("in.acm","r",stdin);
35     int t;cin>>t;
36     while(t--)
37     {
38         int R,l,r,n,ll,rr;
39         sum=0,ls=1e15;
40         scanf("%d%d",&R,&n);
41         for(int i=1;i<=n;i++)
42             scanf("%d%d%d%d",&rc[i].x,&rc[i].y,&rc[i].w,&rc[i].h),sum+=1LL*rc[i].w*rc[i].h;
43         l=0,r=R;
44         while(l<=r)
45         {
46             int mid=l+r>>1;
47             LL ret=check(mid,n);
48             if(ret>=sum-ret)
49                 ll=mid,r=mid-1;
50             else
51                 l=mid+1;
52         }
53         ls=2LL*check(ll,n)-sum;
54         l=ll,r=R;
55         while(l<=r)
56         {
57             int mid=l+r>>1;
58             LL ret=check(mid,n);
59             if(2LL*ret-sum<=ls)
60                 rr=mid,l=mid+1;
61             else
62                 r=mid-1;
63         }
64         printf("%d\n",rr);
65     }
66     return 0;
67 }

 

posted @ 2017-10-18 15:40  weeping  阅读(227)  评论(0编辑  收藏  举报