Codeforces_750_C_(二分查找)
2 seconds
256 megabytes
standard input
standard output
Every Codeforces user has rating, described with one integer, possibly negative or zero. Users are divided into two divisions. The first division is for users with rating 1900 or higher. Those with rating 1899 or lower belong to the second division. In every contest, according to one's performance, his or her rating changes by some value, possibly negative or zero.
Limak competed in n contests in the year 2016. He remembers that in the i-th contest he competed in the division di(i.e. he belonged to this division just before the start of this contest) and his rating changed by ci just after the contest. Note that negative ci denotes the loss of rating.
What is the maximum possible rating Limak can have right now, after all n contests? If his rating may be arbitrarily big, print "Infinity". If there is no scenario matching the given information, print "Impossible".
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000).
The i-th of next n lines contains two integers ci and di ( - 100 ≤ ci ≤ 100, 1 ≤ di ≤ 2), describing Limak's rating change after the i-th contest and his division during the i-th contest contest.
If Limak's current rating can be arbitrarily big, print "Infinity" (without quotes). If the situation is impossible, print "Impossible" (without quotes). Otherwise print one integer, denoting the maximum possible value of Limak's current rating, i.e. rating after the n contests.
3
-7 1
5 2
8 2
1907
2
57 1
22 2
Impossible
1
-5 1
Infinity
4
27 2
13 1
-50 1
8 2
1897 题意:n场比赛,Ci表示第i场比赛后rating的改变量,Di表示第i场比赛开始时选手所属div。问n场比赛结束时选手的最大rating。
div1:rating>1899;div2:rating<=1899。 看着是第三道题,以为比较水,掉以轻心,题也没读仔细,思路也没有,只想了模拟。认真审题,认真分析条件啊!!! 思路:模拟应该是不行的,因为对于第i场比赛开始(或结束)时的rating,不仅由[1,i-1]场比赛决定,而且还与第i+1场比赛开始
时div有关。 看了CF上别人给这道题贴的标签,恍然大悟,二分查找,(但是由于头天晚上太晚,头脑不清醒还是没有做出来)。
第二天轻松A掉。
二分查找:最多200000场比赛,每场rating变化值[-100,100],从[-20000000+1899,20000000+2000]之间二分查找,依次验证
是否合法,倒序模拟每一场比赛,若能有(-20000000+1899)<=res<=(20000000+1900),那么有解;若res>=20001900,则无限大;
其他情况则不可能(对于不可能的情况,二分查找时,一定可以在一个区间内结束查找,且无解,此处可以想一想为啥)。 代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
#define INF 20000000
#define N 200005 int change[N],Div[N];
int n; int relation(int num)
{
for(int i=n;i>=;i--)
{
num-=change[i];
if(Div[i]==&&num<)
return -;
else if(Div[i]==&&num>)
return ;
} return ;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d%d",&change[i],&Div[i]);
int l=-,r=+,res=-(INF+),mid;
while(l<=r)
{
mid=(l+r)>>;
if(relation(mid)==)
r=mid-;
else if(relation(mid)==-)
l=mid+;
else
{
res=mid;
l=mid+;
}
}
//cout<<"mid:"<<mid<<endl;
//cout<<"res:"<<res<<endl; if(res>=INF+)
printf("Infinity\n");
else if(res>=-+)
printf("%d\n",res);
else
printf("Impossible\n");
}
return ;
}
Codeforces_750_C_(二分查找)的更多相关文章
- jvascript 顺序查找和二分查找法
第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...
- Java实现的二分查找算法
二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...
- 从一个NOI题目再学习二分查找。
二分法的基本思路是对一个有序序列(递增递减都可以)查找时,测试一个中间下标处的值,若值比期待值小,则在更大的一侧进行查找(反之亦然),查找时再次二分.这比顺序访问要少很多访问量,效率很高. 设:low ...
- java实现二分查找
/** * 二分查找 * @param a * @param n * @param value * @return * @date 2016-10-8 * @author shaobn */ publ ...
- 最新IP地址数据库 二分逼近&二分查找 高效解析800万大数据之区域分布
最新IP地址数据库 来自 qqzeng.com 利用二分逼近法(bisection method) ,每秒300多万, 比较高效! 原来的顺序查找算法 效率比较低 readonly string i ...
- c#-二分查找-算法
折半搜索,也称二分查找算法.二分搜索,是一种在有序数组中查找某一特定元素的搜索算法. A 搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: B 如果某一特定元素大于或者小 ...
- 【Python】二分查找算法
二分查找:在一段数字内,找到中间值,判断要找的值和中间值大小的比较.如果中间值大一些,则在中间值的左侧区域继续按照上述方式查找.如果中间值小一些,则在中间值的右侧区域继续按照上述方式查找.直到找到我们 ...
- PHP实现文本快速查找 - 二分查找
PHP实现文本快速查找 - 二分查找法 起因 先说说事情的起因,最近在分析数据时经常遇到一种场景,代码需要频繁的读某一张数据库的表,比如根据地区ID获取地区名称.根据网站分类ID获取分类名称.根据关键 ...
- java二分查找举例讨论
最近做笔试题有这么一个关于二分查找的例子. 给一个有序数组,和一个查找目标,用二分查找找出目标所在index,如果不存在,则返回-1-(其应该出现的位置),比如在0,6,9,15,18中找15,返回3 ...
随机推荐
- C# .NET 如何修改代码字体
工具-选项-字体和颜色
- redux 存值 及 取值 的操作
项目目录 首先,一个基于React + Redux + React-Router的项目目录可以按照我下方的图片来构建: 其中assets目录用于存放项目的静态资源,如css/图片等,src目录则用于存 ...
- cocos2d-x 3.6版连连看版本号控制
为了以后的开发和管理.源代码开发必须要使用版本号控制.我们当然选择git来做版本号控制了. 假设你在终端输入git,提示不是一个命令的话.那就说明你的机器没有安装git工具.那就安装一个,百度之有非常 ...
- Struts2 整合jQuery实现Ajax功能(1)
技术领域非常多东西流行,自然有流行的道理.这几天用了jQuery,深感有些人真是聪明绝顶,能将那么多技术融合的如此完美. 首先明白个概念: jQuery是什么:是使用javascript语言开发的,用 ...
- ubuntu双网卡绑定配置
1,安装bonding需要的软件 sudo apt-get install ifenslave 2,在/etc/modules中加入: bonding mode= miimon= 3,在/etc/ne ...
- go10---struct
package main import ( "fmt" ) type test struct{} //空的结构体 type person struct { name string ...
- 获得了Root权限后Read-only file system
获得了Root权限后,adb shell进入文件系统,有时仍然不能对系统文件夹进行写操作,典型的如删除/system/app下的Apk, 例如系统报:rm failed for xxx.apk, Re ...
- D1 模拟赛
T1 note 数组开小 菜的真实 60分 题目大意: 一个字符串 分成若干段 使每段内都没有重复的字符 求最少的段数 思路: 可以贪心 #include<iostream> #inclu ...
- oracle创建默认表空间---重要
当oracle创建数据库后,sys创建用户时还要有默认表空间.不创建默认表空间在导如项目时会有些数据表导入不成功! 由于时间仓促以截屏为例 之后会在刚刚那个空文件生成一个文件 ----------- ...
- 1617: [Usaco2008 Mar]River Crossing渡河问题(dp)
1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1219 Solved: ...