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
In the first sample, the following scenario matches all information Limak remembers and has maximum possible final rating:
- Limak has rating 1901 and belongs to the division 1 in the first contest. His rating decreases by 7.
- With rating 1894 Limak is in the division 2. His rating increases by 5.
- Limak has rating 1899 and is still in the division 2. In the last contest of the year he gets + 8 and ends the year with rating 1907.
In the second sample, it's impossible that Limak is in the division 1, his rating increases by 57 and after that Limak is in the division 2 in the second contest.
自己当时的思路是,二分,发现没法用,然后想枚举,WA了,听到别人的思路,明白了就是一直在求解不等式
如例子1:假设 初始值是x
-7 1 --> x>=1900 -->x属于 [1900,+00)
5 2 --> x-7<=1899 -->x属于 (-00,1906]
8 2 --> x-7+5<=1899-->x属于(-00,1901]
我们的x值要尽量的大,但是我们的范围取值都必须得满足,也就是说我们只能取到所有右端点的最小值。同理,我们只能所有左端点的最大值。
如果说这两个值没有公共的部分,就说明不可能,如果说右端点是+00(正无穷),的话,就是Infinity
#include <cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll INF=0x3f3f3f3f;
int main()
{
ll sum=0;
int n;
ll left_pos=-INF,right_pos=INF;
ll initial;
scanf("%d",&n);
int val,gra;
for(int i=1;i<=n;i++){
scanf("%d %d",&val,&gra);
if(i==1){
if(gra==1)
left_pos=1900;
else
right_pos=1899;
sum += val;
}
else{
if(gra==1){
initial = 1900 -sum;
left_pos=max(left_pos,initial);
}
else{
initial=1899-sum;
right_pos=min(right_pos,initial);
}
sum+=val;
}
}
if(right_pos==INF)
printf("Infinity\n");
else if(right_pos<left_pos)
printf("Impossible\n");
else
printf("%I64d\n",right_pos+sum);
return 0;
}