UVALive 7261 Xiongnu's Land(二分)

来源:互联网 发布:python 判断加载完成 编辑:程序博客网 时间:2024/06/08 23:36

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5273

思路:二分位置(无需考虑总坐标,仅考虑横坐标即可),使得2*area >= sum,在满足该条件的情况下,尽量右移使得左侧面积尽量大。

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define debuusing namespace std;const int maxn=1e4+50;typedef long long LL;struct Node{    int l,r,h;};int t,R,n;Node a[maxn];LL cal(int x){    LL sum=0;    for(int i=0; i<n; i++)        if(a[i].l<x)            sum+=(LL)(min(a[i].r,x)-a[i].l)*a[i].h;    return sum;}int main(){#ifdef debug    freopen("in.in","r",stdin);#endif // debug    scanf("%d",&t);    while(t--)    {        LL sum=0;        scanf("%d",&R);        scanf("%d",&n);        for(int i=0; i<n; i++)        {            int x,y,w,h;            scanf("%d%d%d%d",&x,&y,&w,&h);            a[i].l=x,a[i].r=x+w,a[i].h=h;            sum+=(LL)w*h;        }        int l=0,r=R,mid;        while(l<r)        {            mid=(l+r)/2;            LL area=cal(mid);            if(2*area<sum) l=mid+1;            else r=mid;        }        LL tmp=cal(r);        while(cal(r)==tmp&&r<=R) r++;        printf("%d\n",r-1);    }    return 0;}



0 0