题目链接:http://hihocoder.com/problemset/problem/1249
题目大意:有一个大正方形里面有好多不重叠的小矩形,怎么找出一条竖线分割这个正方形,使得两边的矩形面积尽量相等并且正方形左边的面积比右边大
思路:做两次二分就好了
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define xx first
#define yy second
struct node{
int len,wid;
long long sum;
pair<int,int> p1;
pair<int,int> p2;
void cal(){
p2.xx=p1.xx+len;
p2.yy=p1.yy-wid;
sum=(long long)len*wid;
}
};
bool cmp(const node&a,const node&b){
return a.p1.xx<b.p1.xx;
}
const int maxn=1e4+;
node a[maxn];
int n,r;
long long check(int x){
long long tmp=;
for(int i=;i<=n;i++){
tmp+=((long long )a[i].wid*max(min(a[i].len,x-a[i].p1.xx),));
}
return tmp;
}
int binary_search(int R,long long sum){
int l=-,r=R+;
int mid;
while(r-l>){
mid=(l+r)>>;
long long tmp=check(mid);
if(tmp>=sum-tmp){
r=mid;
}
else l=mid;
}
long long ans=check(r);
l=-,r=R+;
while(r-l>){
mid=(l+r)>>;
long long tmp=check(mid);
if(tmp>ans) r=mid;
else l=mid;
}
return l;
}
int main(){
int T;
//freopen("C:\\Users\\acm\\Desktop\\2015Beijing\\in.txt","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%d",&r);
scanf("%d",&n);
long long sum=;
for(int i=;i<=n;i++){
int x,y,len,wid;
scanf("%d %d %d %d",&x,&y,&len,&wid);
sum+=((long long)len*wid);
a[i].len=len,a[i].wid=wid,a[i].p1=make_pair(x,y),a[i].cal();
}
sort(a+,a++n,cmp);
printf("%d\n",binary_search(r,sum));
}
return ;
}