2015icpc北京 A.Xiongnu's Land

Wei Qing(died 106 BC) was a military general of the Western Han dynasty whose campaignsagainst the Xiongnu earned him great acclaim. He was a relative of Emperor Wubecause he was the younger half-brother of Empress Wei Zifu (Emperor Wu’s wife)and the husband of Princess Pingyang. He was also the uncle of Huo Qubing,another notable Han general who participated in the campaigns against theXiongnu and exhibited outstanding military talent even as a teenager.

Defeated by Wei Qingand Huo Qubing, the Xiongnu sang: “Losing my Qilian Mountains, made my cattleunthriving; Losing my Yanzhi Mountains, made my women lacking rouge.”

The text above is digested from Wikipedia. Since Wei and Huo’sdistinguished achievements, Emperor Wu decided to give them some awards — apiece of land taken by them from Xiongnu. This piece of land was located in adesert, and there were many oases in it. Emperor Wu wanted to draw a straightsouth-to-north dividing line to divide the land into two parts, and gave thewestern part to Wei Qing while gave the eastern part to Huo Qubing. There aretwo rules about the land dividing:

1.   The total area of the oases layin Wei’s land must be larger or equal to the total area of the oases lay inHuo’s land, and the difference must be as small as possible.

2.    Emperor Wu wanted Wei’s land to be as large as possible withoutviolating the rule 1.

To simplify theproblem, please consider the piece of land given to Wei and Huo as a square ona plane. The coordinate of its left bottom corner was (0,0) and the coordinate of its right top corner was (R,R). Each oasis in this land could also be considered as a rectanglewhich was parallel to the coordinate axes. The equation of the dividing linewas like x = n, and n must be an integer. If the dividing line split an oasis, then Weiowned the western part and Huo owned the eastern part. Please help Emperor Wuto find out how to draw the dividing line.

Input

The first line of the input is an integer K meaning that there are K (1 ≤ K ≤15) test cases.

For each test case:

   The first line is an integer R, indicating thatthe land’s right top corner was at (R,R) (1 ≤ R ≤1,000,000)

   Then a linecontaining an integer N follows, indicatingthat there were N (0 < N ≤ 10000) oases.

   Then N lines follow, each contains four integers L, T, W and H, meaning that there was an oasis whose coordinate of the left topcorner was (L,T), and itswidth was W and height was H. (0 ≤ L,T R, 0 < W,H R). No oasisoverlaps.

Output

For each test case, print an integer n, meaning that Emperor Wu should draw a dividing line whose equationis x = n. Please notethat, in order to satisfy the rules, Emperor might let Wei get the whole landby drawing a line of x =R if he hadto.

Sample Input

2

1000

2

11 2 1

51 2 1

1000

1

11 2 1

Sample Output

5

2


题意:有一个正方形土地,上面有若干块绿洲。让你以x0为界限划一条竖线,要求左边绿洲面积>=右边绿洲面积且两者面积最接近。另外要求左边的土地总面积最大。求x0

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

ac代码:

#include<bits/stdc++.h>

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()
{
    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;
}


猜你喜欢

转载自blog.csdn.net/ac_blood/article/details/80073004