(UVALive 7261)Xiongnu's Land 二分
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 ;
}
(UVALive 7261)Xiongnu's Land 二分的更多相关文章
- UVALive 7261 Xiongnu's Land (扫描线)
Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against the ...
- UVALive - 7261 Xiongnu's Land
思路: 先二分下界,再二分上届. #include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB ...
- 二分+贪心 hihocoder 1249 Xiongnu's Land (15北京A)
题目传送门 题意:有多个矩形分布在[0, 0]到[R, R]的的范围内,画一条竖线分割成两块矩形,使得左边包括矩形的面积大于等于右边的面积,在这个前提下使得画的竖线尽量远 分析:二分答案,当面积相等时 ...
- [ An Ac a Day ^_^ ] HihoCoder 1249 Xiongnu's Land 线性扫描
拿到了icpc北京站的参赛名额 感谢亮哥~ 虽然是地狱之战 但也要全力以赴! 题意: 有一片沙漠 n片绿洲 让你用一条线分成两部分 左≥右 而且分割线要尽量靠右 问线的位置 思路: 网上说可以二分 没 ...
- Gym 101194D / UVALive 7900 - Ice Cream Tower - [二分+贪心][2016 EC-Final Problem D]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- 2015北京区域赛 Xiongnu's Land
Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against the ...
- UVALive 6656 Watching the Kangaroo --二分
题意:给你一些区间,再查询一些点,问这些点与所有区间形成的最小距离的最大值.最小距离定义为:如果点在区间内,那么最小距离为0,否则为min(pos-L[i],R[i]-pos). 解法:当然要排个序, ...
- hdu-----(1507)Uncle Tom's Inherited Land*(二分匹配)
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- UVALive 3635 Pie 切糕大师 二分
题意:为每个小伙伴切糕,要求每个小盆友(包括你自己)分得的pie一样大,但是每个人只能分得一份pie,不能拿两份凑一起的. 做法:二分查找切糕的大小,然后看看分出来的个数有没有大于小盆友们的个数,它又 ...
随机推荐
- head first c<11>初探网络编程上
server连接网络四部曲. 为了与外界沟通,c程序用数据流读写字节.比較经常使用的数据流有标准输入.标准输出.文件等. 假设想写一个与网络通信的程序.就须要一种新的数据流----------套接字. ...
- Pimp_my_Z1
https://github.com/Androguide/Pimp_my_Z1 Pimp_my_Z1-master.zip
- CircleDisplay
https://github.com/PhilJay/CircleDisplay
- gdb在运行maintenance info program-spaces命令时coredump
coredump时的信息: (gdb) maintenance info program-spaces *** Error in `gdb': free(): invalid pointer: 0x0 ...
- 关于jquery ID选择器的一点看法
最近看到一道前端面试题: 请优化selector写法:$(".foo div#bar:eq(0)") 我给出的答案会是: 1. $("#bar") 2. $( ...
- php中如何使用phpredis
安装redis服务:下载地址:http://redis.io/download,下载最新文档版本.本教程使用的最新文档版本为 2.8.17,下载并安装: $ wget http://download. ...
- java_小技巧
看很多人说,在Eclipse里面,输入Syso然后按 ALT+/不起作用. 正确的用法如下,先输入一行 System.out.println(); 然后连按5次以上shift键,其实就是粘滞的功能.接 ...
- [Javascript,JSON] JQuery处理json与ajax返回JSON实例
转自:http://www.php100.com/html/program/jquery/2013/0905/5912.html [导读] json数据是一种经型的实时数据交互的数据存储方法,使用到最 ...
- CentOS(八)--crontab命令的使用方法
crontab命令常见于Unix和Linux的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于"crontab"文件中,以供之后读取和执行. 在 ...
- [改善Java代码]养成良好习惯,显式声明UID
建议11: 养成良好习惯,显式声明UID 我们编写一个实现了Serializable接口(序列化标志接口)的类, Eclipse马上就会给一个黄色警告:需要增加一个Serial Version ID. ...