C. New Year and Rating
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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".

Input

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.

Output

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.

Examples
input
3
-7 1
5 2
8 2
output
1907
input
2
57 1
22 2
output
Impossible
input
1
-5 1
output
Infinity
input
4
27 2
13 1
-50 1
8 2
output
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_(二分查找)的更多相关文章

  1. jvascript 顺序查找和二分查找法

    第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...

  2. Java实现的二分查找算法

    二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...

  3. 从一个NOI题目再学习二分查找。

    二分法的基本思路是对一个有序序列(递增递减都可以)查找时,测试一个中间下标处的值,若值比期待值小,则在更大的一侧进行查找(反之亦然),查找时再次二分.这比顺序访问要少很多访问量,效率很高. 设:low ...

  4. java实现二分查找

    /** * 二分查找 * @param a * @param n * @param value * @return * @date 2016-10-8 * @author shaobn */ publ ...

  5. 最新IP地址数据库 二分逼近&二分查找 高效解析800万大数据之区域分布

    最新IP地址数据库  来自 qqzeng.com 利用二分逼近法(bisection method) ,每秒300多万, 比较高效! 原来的顺序查找算法 效率比较低 readonly string i ...

  6. c#-二分查找-算法

    折半搜索,也称二分查找算法.二分搜索,是一种在有序数组中查找某一特定元素的搜索算法. A 搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: B 如果某一特定元素大于或者小 ...

  7. 【Python】二分查找算法

    二分查找:在一段数字内,找到中间值,判断要找的值和中间值大小的比较.如果中间值大一些,则在中间值的左侧区域继续按照上述方式查找.如果中间值小一些,则在中间值的右侧区域继续按照上述方式查找.直到找到我们 ...

  8. PHP实现文本快速查找 - 二分查找

    PHP实现文本快速查找 - 二分查找法 起因 先说说事情的起因,最近在分析数据时经常遇到一种场景,代码需要频繁的读某一张数据库的表,比如根据地区ID获取地区名称.根据网站分类ID获取分类名称.根据关键 ...

  9. java二分查找举例讨论

    最近做笔试题有这么一个关于二分查找的例子. 给一个有序数组,和一个查找目标,用二分查找找出目标所在index,如果不存在,则返回-1-(其应该出现的位置),比如在0,6,9,15,18中找15,返回3 ...

随机推荐

  1. 策略模式&反射

    业务代码 class Operate { public string _firstKey; public string _secondKey; public string _extendKey; pu ...

  2. Codeforces Beta Round #2 B. The least round way

    这个2B题还好~~ 题目大意: 给出一个矩阵.从左上走到右下,仅仅能往右或下走.路径中每一个格子有一个数.这些数相乘得出一个数. 求这个数末尾零最少的一条路径. 解题思路: 找出一条路径.乘积得数中素 ...

  3. 一起talk C栗子吧(第七回:C语言实例--进制转换)

    各位看官们.大家好,从今天開始.我们讲大型章回体科技小说 :C栗子,也就是C语言实例. 闲话休提, 言归正转.让我们一起talk C栗子吧! 看官们.上一回中咱们说的是生成随机数的样例.这一回咱们说的 ...

  4. Msql入门实战之下

    前面一章主要解说了mysql的select的使用方法.将select的大部分使用方法进行分别解说.本章主要解说Msql约束表的建立,以及存储过程的实现,附带其它介绍.临时就算入门了,Mysql索引之后 ...

  5. 2015ACM/ICPC Asia Regional Changchun Online /HDU 5438 图

    Ponds                                   Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 1310 ...

  6. overwrite 复制

    [root@myv xiaole_dl_img]# cp upfc/mainDEBUGmysqllogTEST.py online_package_test_/tmp/ cp: overwrite ‘ ...

  7. HDU 5753Permutation Bo

    Permutation Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  8. SQl 事物+视图+游标+索引+锁

    一:事务: 是访问并可能更新数据库中各种数据项的一个程序执行单元(unit),事务是恢复和并发控制的基本单位. 事务的四个特性:ACID A:atomicity 原子性,事务里的所有操作都是一体的,要 ...

  9. luogu 3951 小凯的疑惑

    noip2017 D1T1 小凯的疑惑 某zz选手没有看出这道结论题,同时写出了exgcd却不会用,只能打一个哈希表骗了30分 题目大意: 两个互质的正整数a和b,求一个最小的正整数使这个数无法表示为 ...

  10. Flink源码阅读(1.7.2)

    目录 Client提交任务 flink的图结构 StreamGraph OptimizedPlan JobGraph ExecutionGraph flink部署与执行模型 Single Job Jo ...