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

Description

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.

Input

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)

oases.

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. 

 

output

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

2

1000

2

1 1 2 1

5 1 2 1

1000

1

1 1 2 1

Sample Output

5

2


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


这题看到数据范围因为多测可以猜想做法应该是O(R)。那么考虑扫描线

对于一个绿洲L T W H

我们对每个横坐标记录一个增加值ci

那么c[L]+=H c[L+W]-=H

然后对C[L]累合就可以求出到某个坐标的绿洲面积

然后就可以求出ans了

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct green
{
	int x,y,l,h;
}a[200001];
long long c[2000001];
int main()
{
	int T;
	scanf("%d",&T);
	while(T>0)
	{
		T--;
		int n,m;
		scanf("%d%d",&n,&m);
		int i;
		long long sum=0;
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%d%d",&a[i].x,&a[i].y,&a[i].l,&a[i].h);
			sum+=(long long)a[i].l*(long long)a[i].h;
		}
		sum=(sum+(long long)1)/(long long)2;
		memset(c,0,sizeof(c));
		for(i=1;i<=m;i++)
		{
			c[a[i].x]+=a[i].h;
			c[a[i].x+a[i].l]-=a[i].h;
		}
		long long dx=c[0],ss=0;
		int ans;
		for(i=1;i<=n;i++)
		{
			ss+=dx;
			if(ss>=sum)
			{
				ans=i;
				dx+=c[i];
				while(dx==0&&ans<n)
				{
					ans++;
					dx+=c[ans];
				}
				break;
			}
			dx+=c[i];
		}
		printf("%d\n",ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值