UVALive7261(2015ACM/ICPC北京赛区现场赛A)
思路:二分枚举x。
#include <cstdio>
using namespace std;
const int MAXN = ;
typedef long long LL;
struct Rectangle{
int l, t, w, h;
LL area;
}rect[MAXN];
int R, n;
LL sum;
LL getArea(int x)
{
LL ret = ;
for(int i = ; i < n; i++)
{
if(rect[i].l+rect[i].w <= x)
{
ret += rect[i].area;
}
else if(rect[i].l < x)
{
ret += ((LL)(x - rect[i].l) * rect[i].h);
}
}
return ret;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
sum = ;
scanf("%d %d", &R, &n);
for(int i = ; i < n; i++)
{
scanf("%d %d %d %d", &rect[i].l, &rect[i].t, &rect[i].w, &rect[i].h);
rect[i].area = ((LL)rect[i].w * rect[i].h);
sum += rect[i].area;
}
if(sum == )
{
printf("%d\n", R);
continue;
}
//尽可能使x两边的oases的面积之和相等
int left = -, right = R;//求right.将right初始化为可能的答案,left初始化为取不到的数值.
while(right - left > )
{
int mid = (left + right) >> ;
LL sl = getArea(mid);
LL sr = sum - sl;
if(sl >= sr)
{
right = mid;
}
else
{
left = mid;
}
} //尽可能使x靠右
int s = getArea(right);
left = right, right = R+;//求left.将left去初始化为可能的答案,将right初始化为取不到的答案.
while(right - left > )
{
int mid = (left + right) >> ;
if(getArea(mid) <= s)
{
left = mid;
}
else
{
right = mid;
}
}
printf("%d\n", left);
}
return ;
}
二分答案的过程中左右区间的划分问题。大于等于答案的最左值 mid = (left + right) >> 1; 小于等于答案的最右值 mid = (left + right + 1) >> 1;
#include <cstdio>
using namespace std;
const int MAXN = ;
typedef long long LL;
struct Rect{
int l, t, w, h;
}rect[MAXN];
int n, R;
LL sum;
LL getArea(int x)
{
LL area = ;
for(int i = ; i < n; i++)
{
if(rect[i].l + rect[i].w <= x)
{
area += ((LL)rect[i].w * rect[i].h);
}
else if(rect[i].l < x)
{
area += ((LL)rect[i].h * (x - rect[i].l));
}
}
return area;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
sum = ;
scanf("%d %d", &R, &n);
for(int i = ; i < n; i++)
{
scanf("%d %d %d %d", &rect[i].l, &rect[i].t, &rect[i].w, &rect[i].h);
sum += ((LL)rect[i].w * rect[i].h);
} int left = , right = R;
while(right > left)
{
int mid = (left + right) >> ; //求大于等于答案的最左值
LL s1 = getArea(mid);
LL s2 = sum - s1;
if(s1 >= s2)
{
right = mid;
}
else
{
left = mid + ;
}
} LL tag = getArea(right);
left = right, right = R;
while(right > left)
{
int mid = (left + right + ) >> ; //求小于等于答案的最右值
if(getArea(mid) <= tag)
{
left = mid;
}
else
{
right = mid - ;
}
}
printf("%d\n", left);
}
return ;
}
UVALive7261(2015ACM/ICPC北京赛区现场赛A)的更多相关文章
- ACM总结——2017ACM-ICPC北京赛区现场赛总结
现在距离比赛结束已经过了一个多星期了,也是终于有时间写下心得了.回来就是被压着做项目,也是够够的. 这次比赛一样是我和两个学弟(虽然是学弟,但我的实力才是最弱的T_T)一起参加的,成绩的话打铁,算是情 ...
- 2014 ACM/ICPC 鞍山赛区现场赛 D&I 解题报告
鞍山现场赛结束了呢-- 我们出的是D+E+I三道题-- 吾辈AC掉的是D和I两道,趁着还记得.先在这里写一写我写的两道水题D&I的解题报告吧^_^. D题的意思呢是说星云内有一堆排成一条直线的 ...
- 2014年亚洲区域赛北京赛区现场赛A,D,H,I,K题解(hdu5112,5115,5119,5220,5122)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 下午在HDU上打了一下今年北京区域赛的重现,过了5题,看来单挑只能拿拿铜牌,呜呜. ...
- HDU 5120 A Curious Matt(2014北京赛区现场赛A题 简单模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5112 解题报告:扫一遍 #include<cstdio> #include<cstr ...
- HDU 5120 Intersection(2014北京赛区现场赛I题 计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5120 解题报告:给你两个完全相同的圆环,要你求这两个圆环相交的部分面积是多少? 题意看了好久没懂.圆环 ...
- HDU 4793 Collision (解二元一次方程) -2013 ICPC长沙赛区现场赛
题目链接 题目大意 :有一个圆硬币半径为r,初始位置为x,y,速度矢量为vx,vy,有一个圆形区域(圆心在原点)半径为R,还有一个圆盘(圆心在原点)半径为Rm (Rm < R),圆盘固定不动,硬 ...
- [hdu5113]Black And White2014北京赛区现场赛B题(搜索加剪枝)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Black And White Time Limit: 2000/2000 MS ...
- Substrings 第37届ACM/ICPC 杭州赛区现场赛C题(hdu 4455)
http://acm.hdu.edu.cn/showproblem.php?pid=4455 https://icpcarchive.ecs.baylor.edu/index.php?option=c ...
- hdu 4431 第37届ACM/ICPC 天津赛区现场赛A题 枚举
题意:就是给了13张牌.问增加哪些牌可以胡牌.m是数字,s是条,p是筒,c是数字 胡牌有以下几种情况: 1.一个对子 + 4组 3个相同的牌或者顺子. 只有m.s.p是可以构成顺子的.东西南北这样 ...
随机推荐
- 【2018 “百度之星”程序设计大赛 - 初赛(B)- 1001】degree
Problem Description 度度熊最近似乎在研究图论.给定一个有 N 个点 (vertex) 以及 M 条边 (edge) 的无向简单图 (undirected simple graph) ...
- LeetCode OJ : Different Ways to Add Parentheses(在不同位置增加括号的方法)
Given a string of numbers and operators, return all possible results from computing all the differen ...
- WAF的实现
文章来源:http://danqingdani.blog.163.com/blog/static/1860941952014101723845500/ 本篇文章从WAF产品研发的角度来YY如何实现一款 ...
- APUE学习笔记——5缓冲Buffering、流、文件对象
缓冲的几个基本概念 缓冲的作用:减少系统read和write的次数. 全缓冲 系统标准I/O缓冲区被写满时才进行真正的I/O操作. 磁盘文件一般使用全缓冲 ...
- LINUX系统下的数据库的管理
环境:配置好IP和YUM源 一.数据库的安装及密码的修改 [1]yum install mariadb-server -y ##安装mariadb数据库 [2]systemctl ...
- 如何将dom4j的jar包放到java项目中
---恢复内容开始--- 如果你建的是java project项目的话,那就通过Myeclispe中的右键你的项目名称,选择build path ->config build path... 然 ...
- TCP 初步认识
TCP连接的建立---三次握手 第一次握手:客户端TCP首先给服务器端TCP发送一个特殊的TCP数据段. 该数据段不包含应用层数据,并将头部中的SYN位设置为1,所以该数据段被称为SYN数据段. 另外 ...
- LR11开始录制时打不开浏览器
LR11 能支持的浏览器实际上不仅限于 IE8 官方文档没有明确表示支持,可以尝试.官方支持列表如下: ➤ Microsoft Internet Explorer 6.0 SP1 or SP2 ➤ M ...
- SQL基础四(例子)
------------------------------------------------ --分别创建student/course/score表 Create table student ( ...
- caffe官网的部分翻译及NG的教程
Caffe原来叫:Convolutional Architecture for Fast Feature Embedding 官网的个人翻译:http://blog.csdn.net/fengbing ...