码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive 7261 Xiongnu's Land(二分)

时间:2016-08-10 21:12:02      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

题目地址: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 debu
using 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;
}



UVALive 7261 Xiongnu's Land(二分)

标签:

原文地址:http://blog.csdn.net/wang2147483647/article/details/52176233

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!