codeforces contest 750 c题

来源:互联网 发布:excel未知网络错误 编辑:程序博客网 时间:2024/04/28 22:53

C. New Year and Rating
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
Note
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.

题意:排名可以分为两部分,大于等于1900的为第一部分,其他的为第二部分,一个人经历了n场比赛,给你每场比赛之前这个人的排名在哪一部分,然后给你这场比赛后这个人的排名变化,问你最后这个人的排名的最大可能是多少,如果是无穷大,则输出Infinity,如果没有这种情况就输出Impossible,反之输出最大的排名。

解题思路:做的时候首先就想到了二分答案,然后从后往前开始验证,但是却没有考虑一个数不满足条件,那么可能有两种情况,而且这两种情况要分别处理,我当时做的时候,不满足的话当成一种情况处理,所以连样例都过不了,其实,不满足条件的话,一是这个数太大了,二是太小了。然后无穷大的情况就是所有的状态都是1,只要有一个状态2则不可能无穷大。

#include<bits/stdc++.h>using namespace std;const int maxn = 2e5 + 10;int inf = 3e7;int n;int c[maxn];int d[maxn];int ok(int x){    for(int i = n; i >= 1; i--)    {        x = x - c[i];        if(d[i] == 2)        {            if(x > 1899) return 1;//太大        }        else if(d[i] == 1)        {            if(x < 1900) return -1;//太小        }    }    return 0;}int main(){    while(~scanf("%d",&n))    {        bool flag = true;        for(int i = 1; i <= n; i++)        {            scanf("%d%d",&c[i],&d[i]);            if(d[i] == 2) flag = false;        }        if(!flag)        {           int l = -inf;           int r = inf;           int result = -(inf + 1);           while(l <= r)           {               int mid = (l + r)>>1;               int res = ok(mid);               if(res == 1)               {                   r = mid - 1;               }               else if(res == -1)               {                   l = mid + 1;               }               else               {                   result = mid;                   l = mid + 1;               }           }           if(result == -(inf + 1)) printf("Impossible\n");           else printf("%d\n",result);        }        else printf("Infinity\n");    }    return 0;}
1 0
原创粉丝点击