(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,不能拿两份凑一起的. 做法:二分查找切糕的大小,然后看看分出来的个数有没有大于小盆友们的个数,它又 ...
随机推荐
- androidstudio can't run git.exe
今天用android studio从git下载项目的时候遇到一个问题,提示说can't run git.exe 问了下度娘以及谷歌.但是无果,后面捣鼓了一阵,下了一个git windows版本后,在a ...
- makefile中的patsubst
函数名称:加前缀函数—addprefix. 函数功能:为“NAMES…”中的每个文件名称加入前缀“PREFIX”.參数“NAMES…”是空格切割的文件名称序列,将“SUFFIX”加入到此序列的每个文件 ...
- Thread+Handler 线程 消息循环(转载)
近来找了一些关于android线程间通信的资料,整理学习了一下,并制作了一个简单的例子. andriod提供了 Handler 和 Looper 来满足线程间的通信.例如一个子线程从网络上下载了一副图 ...
- mysql 5.1 到 mysql 5.2的出现的索引BTREE问题 use near 'USING BTREE
转自:http://hi.baidu.com/our_poll/item/669c5ce885b33ff1e0a5d4fc 我本机测试是安装的 mysql 5.1 , 但服务器上确是使用的 mysql ...
- java 技术体系
- php中序列化与反序列化在utf8和gbk编码中测试
在php中如果我们统一编码是没有什么问题了,但是很多朋友会发现一个问题就是utf8和gbk编码中返回的值会有所区别: php 在utf8和gbk编码下使用serialize和unserialize互相 ...
- 读logback源码系列文章(五)——Appender --转载
原文地址:http://kyfxbl.iteye.com/blog/1173788 明天要带老婆出国旅游几天,所以这段时间暂时都更新不了博客了,临走前再最后发一贴 上一篇我们说到Logger类的inf ...
- Linux 内核Coding Style整理
转载:http://www.cnblogs.com/wang_yb/p/3532349.html 总结linux内核开发的coding style, 便于以后写代码时参考. 下面只是罗列一些规则, 具 ...
- Josephina and RPG
Josephina and RPG Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Java基础知识强化103:Java常量池理解与总结
一.相关概念 1. 什么是常量 用final修饰的成员变量表示常量,值一旦给定就无法改变! final修饰的变量有三种:静态变量.实例变量和局部变量,分别表示三种类型的常量. 2. Class文件中的 ...