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. js 获取文件本地路径

    1.代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  2. mac中apache+mysql+php+phpMyAdmin配置备忘

    Mac OS X 内置Apache 和 PHP,使用起来非常方便.本文以Mac OS X 10.6.3和为例.主要内容包括: 启动Apache 运行PHP 安装MySQL 使用phpMyAdmin 配 ...

  3. Hiho1041 国庆出游 搜索题解

    题目3 : 国庆出游 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho准备国庆期间去A国旅游.A国的城际交通比較有特色:它共同拥有n座城市(编号1-n): ...

  4. Linux操作系统改动PATH的方法

    1. 暂时改动: 使用export.比如#export PATH=$PATH:/etc/apache/bin 2. 针对用户的改动: vi ~/.bash_profile 增加:export PATH ...

  5. P2P网贷中的4种理財业务模式

     线上3种   直投标:线上理財人直接购买借款人的标.平台仅仅是起个"撮合"作用.收点借款人的服务费.           借款人不还钱,有的平台会帮"借款人" ...

  6. [办公自动化]名师推荐-excelpro刘万祥 图表之道作者

    最早认识刘万祥老师是通过孙小小老师的博客.后来发现制作图表,还真需要和PPT类似,花些时间琢磨一下. 首先你要了解图表的类型,然后需要了解制作方法,最后就是如何美化以及结合PPT等工具帮你分析数据. ...

  7. hbase 增删改查 api 简单操作

    package com.utils; import java.io.IOException; import java.util.ArrayList; import java.util.List; im ...

  8. robot framework运行测试 命令行启动

    ...\rf_test> pybot --test test_case test_suit.robot #运行一条用例 ...\rf_test> pybot test_suit.robot ...

  9. YTU 2543: 数字整除

    2543: 数字整除 时间限制: 1 Sec  内存限制: 128 MB 提交: 33  解决: 8 题目描述 定理:把一个至少两位的正整数的个位数字去掉,再从余下的数中减去个位数的5倍.当且仅当差是 ...

  10. 昆石VOS3000_2.1.2.0完整安装包及安装脚本

    安装包下载地址 http://www.51voip.org/post/57.html 安装教程: 上传安装包 ·给整个目录授权 chmod 777 /root/vosintsall 1.安装前准备 首 ...