首页 > 代码库 > hiho1249 Xiongnu's Land

hiho1249 Xiongnu's Land

题目链接:http://hihocoder.com/problemset/problem/1249

题目大意:有一个大正方形里面有好多不重叠的小矩形,怎么找出一条竖线分割这个正方形,使得两边的矩形面积尽量相等并且正方形左边的面积比右边大

思路:做两次二分就好了

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 #define xx first
 6 #define yy second
 7 struct node{
 8     int len,wid;
 9     long long sum;
10     pair<int,int> p1;
11     pair<int,int> p2;
12     void cal(){
13         p2.xx=p1.xx+len;
14         p2.yy=p1.yy-wid;
15         sum=(long long)len*wid;
16     }
17 };
18 bool cmp(const node&a,const node&b){
19     return a.p1.xx<b.p1.xx;
20 }
21 const int maxn=1e4+10;
22 node a[maxn];
23 int n,r;
24 long long check(int x){
25     long long tmp=0;
26     for(int i=1;i<=n;i++){
27         tmp+=((long long )a[i].wid*max(min(a[i].len,x-a[i].p1.xx),0));
28     }
29     return tmp;
30 }
31 int binary_search(int R,long long sum){
32     int l=-1,r=R+1;
33     int mid;
34     while(r-l>1){
35         mid=(l+r)>>1;
36         long long tmp=check(mid);
37         if(tmp>=sum-tmp){
38             r=mid;
39         }
40         else l=mid;
41     }
42     long long ans=check(r);
43     l=-1,r=R+1;
44     while(r-l>1){
45          mid=(l+r)>>1;
46         long long tmp=check(mid);
47         if(tmp>ans) r=mid;
48         else l=mid;
49     }
50     return l;
51 }
52 int main(){
53     int T;
54     //freopen("C:\\Users\\acm\\Desktop\\2015Beijing\\in.txt","r",stdin);
55     scanf("%d",&T);
56     while(T--){
57         scanf("%d",&r);
58         scanf("%d",&n);
59         long long sum=0;
60         for(int i=1;i<=n;i++){
61             int x,y,len,wid;
62             scanf("%d %d %d %d",&x,&y,&len,&wid);
63             sum+=((long long)len*wid);
64             a[i].len=len,a[i].wid=wid,a[i].p1=make_pair(x,y),a[i].cal();
65         }
66         sort(a+1,a+1+n,cmp);
67         printf("%d\n",binary_search(r,sum));
68     }
69     return 0;
70 }

 

hiho1249 Xiongnu's Land