hihocoder 2015ACM-ICPC 北京赛区 Problem A. Xiongnu's Land

原创 2015年11月18日 23:33:44


Wei Qing (died 106 BC) was a military general of the Western Handynasty whose campaigns against the Xiongnu earned him great acclaim. He was arelative of Emperor Wu because he was the younger half-brother of Empress WeiZifu (Emperor Wu's wife) and the husband of Princess Pingyang. He was also theuncle of Huo Qubing, another notable Han general who participated in thecampaigns against the Xiongnu and exhibited outstanding military talent even asa 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 aboveis digested from Wikipedia. Since Wei and Huo's distinguished achievements,Emperor Wu decided to give them some awards --- a piece of land taken by themfrom Xiongnu. This piece of land was located in a desert, and there were manyoases in it. Emperor Wu wanted to draw a straight south-to-north dividing line todivide the land into two parts, and gave the western part to Wei Qing whilegave the eastern part to Huo Qubing.

There are two rules about the landdividing:


1.  The total area of the oases lay in Wei's land must be larger orequal to the total area of the oases lay in Huo's land, and the difference mustbe 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 coordinateof its right top corner was (R,R).  Eachoasis in this land could also be considered as a rectangle which was parallelto the coordinate axes. The equation of the dividing line was like x = n, and nmust be an integer. If the dividing line split an oasis, then Wei owned thewestern part and Huo owned the eastern part. Please help Emperor Wu to find outhow to draw the dividing line.


The first line of theinput 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, indicatingthat the land's right top corner was at (R,R) (1 <

= R <= 1,000,000)

Then a line containingan integer N follows, indicating that there were N (0 <N <= 10000)


Then N lines follow,each contains four integers  L,T, W andH, meaning that there was an oasis whose coordinate of the left top corner was(L,T), and its width was W and height was H. (0<=L,T <= R, 0<W,H <=R). No oasis overlaps. 



For each testcase, print an integer n, meaning that Emperor Wu should draw a dividing linewhose equation is x = n. Please note that, in order to satisfy the rules , Emperormight let Wei get the whole land by drawing a line of x = R if he had to.  

Sample Input




1 1 2 1

5 1 2 1



1 1 2 1

Sample Output




#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;

ll R, sum;
struct Node
    ll w, h;
    ll a, b;
} f[10009];
int n;

ll fun(ll x)
    ll tmp = 0;
    for(int i = 0; i < n; i++)
        tmp += f[i].h * max(0LL, min(f[i].w, x - f[i].a));
    return tmp;

int main()
    int T;
    scanf("%d", &T);
        scanf("%I64d", &R);
        scanf("%d", &n);
        sum = 0;
        for(int i = 0; i < n; i++)
            scanf("%I64d%I64d%I64d%I64d", &f[i].a, &f[i].b, &f[i].w, &f[i].h);
            sum += (ll)(f[i].w * f[i].h);

        ll le = 0, ri = R + 1;
        ll mid;
        while(le < ri)
            mid = (le + ri) / 2;
            if(fun(mid) * 2 < sum)
                le = mid + 1;
                ri = mid;

         ll ans = fun(ri);
        le = 0, ri = R + 1;
        while(le < ri)
            mid = (le + ri) / 2;
            if(fun(mid) <= ans) le = mid + 1;//再次二分找到使ans最大的位置,此时左边尽量分的多
            else ri = mid;
        int ans1 = le-1;

    return 0;

2015ACM-ICPC 北京赛区 Problem A. Xiongnu's Land

Description Wei Qing (died 106 BC) was a military general of the Western Handynasty whose campaig...
  • lqybzx
  • lqybzx
  • 2016年07月13日 11:06
  • 661

ACM-ICPC北京赛区2015 A hihoCoder 1249 Xiongnu's Land

看了大牛的代码才明白正确的解法是怎样的。一开始想的是只枚举快要到一半的那一部分,就是这个“快要”的判断要了我半条命,WA了N次。大牛的想法很犀利。 总结:对于这种问题,最好就是直接枚举所有情况,再在其...
  • AngOn823
  • AngOn823
  • 2015年11月15日 18:52
  • 478

hihocoder 1249 Xiongnu's Land

题目链接:A题题目大意:给你一个长宽都为k的矩阵,现在给了你一些小矩阵的绿化地带,现在要求你用平行于y轴的线分割这个矩阵为左右两部分,要求: 一、是左边的绿化地带必须大于等于右边的地带,并且两边的绿...
  • Nemaleswang
  • Nemaleswang
  • 2017年10月19日 21:06
  • 69

hihoCoder 1249 Xiongnu's Land(二分)

题目链接:hihoCoder 1249 Xiongnu’s Land代码#include #include #include using namespace std; typedef long l...
  • u011328934
  • u011328934
  • 2015年11月16日 15:38
  • 1118

2015ACM-ICPC 北京赛区 Problem J. Osu! Master

Description   “Osu!” is a unique PC rhythm game, which is very popular over the world. The player s...
  • lqybzx
  • lqybzx
  • 2016年07月13日 13:15
  • 650

2015ACM-ICPC 北京赛区 Problem I. Snake Carpet

Description    In school of EECS of Peking University, there is a homework for all freshman -- the ...
  • lqybzx
  • lqybzx
  • 2016年07月13日 13:01
  • 575


not the end,why the end?
  • u012717411
  • u012717411
  • 2015年11月15日 22:37
  • 899

hiho 1249 Xiongnu's Land

分割线要满足三个条件: 1. 左边的绿洲面积要大于等于右边的绿洲面积。 2.两边的面积的差值要尽可能的小。 3.左边的总面积要尽可能的大于右边。 思路:先二分答案求出条件一和二满足的最左边的分...
  • dml_96
  • dml_96
  • 2015年11月18日 20:34
  • 247

Xiongnu's Land (线性扫描,二分)

Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against th...
  • Littlewhite520
  • Littlewhite520
  • 2017年03月24日 17:10
  • 256

UVA 7261 Xiongnu's Land

U can find the original link herethis question need us to find a line parallel to y axis that divide...
  • lingzidong
  • lingzidong
  • 2017年11月24日 16:19
  • 67
您举报文章:hihocoder 2015ACM-ICPC 北京赛区 Problem A. Xiongnu's Land