(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,不能拿两份凑一起的. 做法:二分查找切糕的大小,然后看看分出来的个数有没有大于小盆友们的个数,它又 ...
随机推荐
- QuickXdev+sublime text打造quick-cocos2d-x开发环境
Sublime 插件安装方法: 一.简单的安装方法 使用Ctrl+`快捷键或者通过View->Show Console菜单打开命令行,粘贴如下代码: import urllib.request, ...
- delphi 去掉TreeView水平滚动条
使用API函数:声明 FUNCTION ulong ShowScrollBar(ulong hwnd,ulong wBar,ulong bShow) LIBRARY "user32. ...
- ios开发——面试篇(一)
面试篇之内存管理与多线程 简述OC中内存管理机制.与retain配对使用的方法是dealloc还是release,为什么?需要与alloc配对使用的方法是dealloc还是release,为 ...
- Linux 内核动态函数调用可视化工具
要求: python .7以上 https://www.python.org/ftp/python/ EG:wget --no-check-certificate https://www.python ...
- js 控制div 显示隐藏的问题
var divs = document.getElementsByTagName("div");得到所有的divfor(var i=0;i<divs.length;i++){ ...
- debian下Vnc
1 VNC(Virtual Network Computing,虚拟网络计算)最早是一套由英国剑桥大学AT&T实验 室在2002年开发的轻量型的远程控制计算机软件,其采用了 GPL 授权条款, ...
- zend studio 函数不提醒 小黄图标 小黄标
在用 Zend Studio 编写 PHP 项目时发现调用系统函数时调试正常, 但是在编写代码时却提示函数未定义"Call to undefined function ", 在左侧 ...
- 基础知识 - Rabin-Karp 算法
Rabin-Karp 算法(字符串快速查找) Go 语言的 strings 包(strings.go)中用到了 Rabin-Karp 算法.Rabin-Karp 算法是基于这样的思路:即把字符串看作是 ...
- Google翻译,3个步骤灭绝人类
今儿这事儿得从一个新闻说起:<谷歌又飙车了,刚发布了神经机器翻译系统,没见过的语言它也能翻译> 大家如果懒的看原文,可以直接看我这个简单白话列表: Google又出来嘚瑟了,发布了基于神经 ...
- python(3)-深浅拷贝
import copy copy.copy() 浅拷贝 copy.deepcopy() 深拷贝 >>> import copy >>> a1 = 123 ...