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是可以构成顺子的.东西南北这样 ...
随机推荐
- C++复习3.C/C++常量的知识
C/C++常量的知识 20130918 语言的实现隐含着使用着一些常量,如初始化全局变量静态变量,另外还有一些我们不曾感觉到的变量:函数地址(也就是函数名称), 静态数组的名字,字符串常亮的地址.常量 ...
- 在 Bash on Ubuntu 上安装Nginx
前言 Win10 上的 Bash on Ubuntu 是个很好用的玩具,让windows开发环境下的人能无缝操练Linux,但是涉及到网络部分还是有很多要该进的地方,比如Nginx的安装就遇到了问题. ...
- vue.js 源代码学习笔记 ----- 工具方法 lang
/* @flow */ // Object.freeze 使得这个对象不能增加属性, 修改属性, 这样就保证了这个对象在任何时候都是空的 export const emptyObject = Obje ...
- c# winform捕获全局异常,并记录日志
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using ...
- spinlock变量没有初始化
http://blog.csdn.net/longwang155069/article/details/52224284
- 常见Git操作及关键知识点
一.Git三区概念 工作区 (work dict) 暂存区(stage)(add 是添加到当前的暂存区) 提交区(就是当前工作的分支master分支或者branches分支) git 所有操作都是基于 ...
- matlab linux的安装(第二次)
1 挂载 2 下载jdk,matlab里面那个不行,缺点东西 3 安装 ./install *** 4 激活,非在线 5 license.lic路径在百度云中有 6 运行路径在你安装过程中的选的路径, ...
- learn go memoization
package main // 参考文章: // https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.12.md i ...
- 【示例代码】 Tuple<T> Func<T>
using System; using System.Math; namespace PiWithMonteCarlo { /// <summary> /// Trivial, synch ...
- 个人博客网站 www.superzhang.site
用django新建了一个博客网站,访问地址为www.superzhang.site.欢迎来逛逛.