Codeforces Good Bye 2016 C. New Year and Rating(模拟)

Description

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

题目大意

有两个等级的比赛div1(分数>=1900)和div2(分数<=1899),每一场增加或者减少ci分,Limak的初始分数是任意的,现Limak参加了n场比赛,判断比赛是否符合情况,并且在符合情况下输出最终可能的最高得分,可为无穷高是输出“Infinity”.

解题思路

以1899和1900两个分数作为基准点进行判断,用两个变量minn和maxx来维护每一场符合情况下的最低分和最高分,维护的方法是:
当第一场为div1时,minn=1900,maxx=inf;当第一场为div2时,minn=-inf,maxx=1899;
对于之后的每一场,用countt值来标记到目前为止所变化的分数,x为可行的初始分数值,
如果为div1,则x需满足x+countt>=1900,即x>=1900-countt,即更新最小值minn=max(minn,1900-countt);
同理为div2时,x需满足x+countt<=1899,即x<=1899-countt,即更新最大值maxx=min(maxx,1899-countt);
如果出现最低分>最高分则说明不符合情况,直接输出“Impossible”;
如果最终maxx或者minn为inf,则说明取值可为无穷大,输出“Infinity”;
否则则说明该样例符合情况,且可取的最大的初始值为maxx,且可得的最大的结果值为maxx+countt。

代码实现

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f3f
#define maxn 200007
ll c[maxn],d[maxn];
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    bool flag=true;
    ll minn=inf*(-1),maxx=inf;
    for(int i=0; i<n; i++)
        cin>>c[i]>>d[i];
    if(d[0]==1)
    {
        minn=1900;
    }
    else
    {
        maxx=1899;
    }
    ll countt=c[0];
    for(int i=1; i<n; i++)
    {
        if(d[i]==1)
        {
            minn=max(minn,1900-countt);
        }
        else
        {
            maxx=min(maxx,1899-countt);
        }
        if(minn>maxx)
        {
            cout<<"Impossible"<<endl;
            flag=false;
            break;
        }
        countt+=c[i];
    }
    if(flag)
    {
        if(minn==inf||maxx==inf)
            cout<<"Infinity"<<endl;
        else
            cout<<maxx+countt<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值