(UVALive 7261)Xiongnu's Land 二分

时间:2023-03-08 23:28:33
(UVALive 7261)Xiongnu's Land  二分

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

Wei Qing (died  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:
. 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.
. Emperor Wu wanted Wei’s land to be as large as possible without violating the rule .
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 (, ) 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 ( ≤ K ≤ ) 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) ( ≤ R ≤
, , )
Then a line containing an integer N follows, indicating that there were N ( < N ≤ ) 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.
( ≤ L, T ≤ R, < 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 Sample Output

题意:有一个矩形的地方左下角坐标(0,0),右上角坐标(R,R),里面有N个绿洲,每个绿洲给出左下角坐标(L,T)宽W高H,把这块地以x=n(整数)分成两部分需要满足两个要求

1.左边与右边的绿洲面积差最小。

2.在满足1的条件下,让左边的面积尽量大

输出分割的坐标n

思路:把矩形分成宽为1的单位矩形,算出每个矩形的绿洲面积,从左边找到左边面积大于等于总面积一半的位置    再向后找,如果面积不变化,坐标向后移,直到面积变化停止

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include <stack>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a))
#define mod 2147493647
#define N 1000010
ll sum[N];
int main()
{
int t,x,y;
ll l,w;
scanf("%d",&t);
int r,n;
while(t--)
{
scanf("%d",&r);
scanf("%d",&n);
ll S=;
met(sum,);
for(int i=; i<n; i++)
{
scanf("%d %d %lld %lld",&x,&y,&w,&l);
S+=w*l;///求总面积
for(int j=x; j<=x+w- && j<=r- ;j++)
{
sum[j]+=l;///记录每个单位的面积变化
}
}
ll ans=;
int i,j;
for(i=; i<r; i++)
{
ans+=sum[i];
if(ans* >= S)///找到一个刚好大于或等于s的地方,如果不能平分,多的一份给左边
break;
}
ll ans1=ans;
for(j=i+;j<r;j++)
{
ans1+=sum[j];
if(ans1!=ans)///如果往后面积绿洲面积没变,西边拥有的面积可以增加
break;
}
printf("%d\n",j);
}
return ;
}