UVALive 7261 A Xiongnu's Land 二分&思路

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/DADDY_HONG/article/details/83345884

Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against the Xiongnu earned him great acclaim. He was a relative of Emperor Wu because 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 the Xiongnu and exhibited outstanding military talent even as a teenager.

Defeated by Wei Qing and Huo Qubing, the Xiongnu sang: “Losing my Qilian Mountains, made my cattle unthriving; Losing my Yanzhi Mountains, made my women lacking rouge.”

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

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

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

To simplify the problem, please consider the piece of land given to Wei and Huo as a square on a 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 rectangle which was parallel to the coordinate axes. The equation of the dividing line was like x = n, and n must be an integer. If the dividing line split an oasis, then Wei owned the western part and Huo owned the eastern part. Please help Emperor Wu to 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 that the land’s right top corner was at (R, R) (1 ≤ R ≤ 1, 000, 000)

Then a line containing an integer N follows, indicating that 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 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.

Output

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

Sample Input

2

1000

2

1 1 2 1

5 1 2 1

1000

1

1 1 2 1

Sample Output

5

2

题目大意:

1.两部分绿洲面积要在保证左边大于等于右边的情况下尽可能差异小;

2.左边的土地面积要尽可能大;

3.划分的x是整数。

学姐说二分,可以二分出满足1时但是右边土地尽可能大;于是,再一次二分,就可以满足条件2了。

感觉思路好蠢。二分也写的好蠢。蠢哭了。

然后发现大家都是扫描线做法。O(n)。真是精妙。本来也这个想法来着2333。

#include <cstdio>
#include <algorithm>
using namespace std;

const int maxr=1e6+10;
const int maxn=1e4+10;

int r,n;
long long s,y;

struct oasis{
     int l,t,w,h;
}o[maxn];

bool cmp(oasis a,oasis b)
{
     if(a.l==b.l) return a.t<b.t;
     return a.l<b.l;
}

long long cal(int x)
{
     long long cnt=0;
     for(int i=0;i<n;++i){
          if(o[i].l>=x) break;
          if(o[i].l+o[i].w>x)
               cnt+=1ll*(x-o[i].l)*o[i].h;
          else
               cnt+=1ll*o[i].w*o[i].h;
          //if(cnt>s) return true;
     }
     return cnt;
}

int main()
{
     int k;
     scanf("%d",&k);
     while(k--){
          s=0;
          scanf("%d%d",&r,&n);
          if(!n){
               printf("%d\n",r);
               continue;
          }
          for(int i=0;i<n;++i){
               scanf("%d%d%d%d",&o[i].l,&o[i].t,&o[i].w,&o[i].h);
               s+=1ll*o[i].w*o[i].h;
          }
          y=s;
          s=(s+1)/2;
          sort(o,o+n,cmp);
          int left=0,right=r,mid;
          while(left<right){
               mid=(left+right)/2;
               long long x=cal(mid);
               if(x<s)
                    left=mid+1;
               else{
                    y=x;
                    right=mid;
               }
          }
          left=right;right=r;
          while(left<right){//printf("l:%d r:%d\n",left,right);
               mid=(left+right+1)/2;
               if(cal(mid)==y)
                    left=mid;
               else
                    right=mid-1;
          }
          printf("%d\n",left);
     }

     return 0;
}

猜你喜欢

转载自blog.csdn.net/DADDY_HONG/article/details/83345884