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 ncontests.
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.
题目大意:
每个人都有一个rating,是一个整数,可以是负数以及0,div1是>=1900分 div2是<=1899,然后有个人参加了N场,比赛,但是只记得每一场是div几,以及每一场赛后的rating的变化量,请问他现在最高可能多少分,如果不可能有符合的情况 输出Impossible,如果可以无限大分数,输出Infinity
分析:
首先提醒一下不要被div1或者div2的界限所困扰。假设初始的分数是x,如果该场他是在div1组内,就有x+presum>=1900.,即x>=1900-presum。这里presum是该场比赛前的rating改变之和。同样的,如果该场他是在div2组内,就有x+presum <= 1899,即x<=1899-presum,presum的意义与上面一样。你的任务就是需要找出符合这些不等式的最大的x,因为最后的presum是固定的。先不要让思维局限在他一开始到底是在div1还是div2,单纯的来看不等式。对于参加div1时候,始终都要满足x>=1900-presum,那么如果参加了很多场div1,最大的x就能满足这些不等式。同理参加div2的时候,每次都要满足不等式x<=1899-presum,最小的x就能满足这么多场div2的不等式。具体解释在代码注释中
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
int main() {
int n;
scanf("%d", &n);
int high_div2 = -INF; // highest prefix sum after which he is in div2
int low_div1 = INF; // lowest prefix sum after which he is in div1
int so_far = 0;
for(int i = 0; i < n; ++i) {
int change, division;
scanf("%d%d", &change, &division);
if(division == 2)
high_div2 = max(high_div2, so_far);
//因为参加div2时都要符合x<=1899-presum,所以最小的x就是符合所有该不等式的,那么就是求此时最大的presum
else
low_div1 = min(low_div1, so_far);
//因为参加div1时要符合x>=1900-presum,所以最大的x就是符合所有该不等式的,那么就是求此时最下的presum
so_far += change;
}
if(high_div2 == -INF)
//如果没有参加过div2就无法知道分数的最大值
puts("Infinity");
else if(high_div2 >= low_div1)
//因为分数x满足x<=1899-presum(div2时的最大)且x>=1900-presum(div1时的最小),如果presum(div2时的最大)>=presum(div1时的最小),显然是无法满足不等式的
puts("Impossible");
else
printf("%d\n", 1899 - high_div2 + so_far);
//既然x的范围1900-presum(div1时的最小)<=x<=1899-presum(div2时的最大),那么就有此式
}