codeforces 750C New Year and Rating

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 ≤ 1001 ≤ 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.



题意:一共参加了n场codeforces比赛,按时间给出参赛记录,每条记录包括ci,di。 ci表示在第i场rating的变化,di有两个可能值:1表示div1(rating>=1900)2表示div2(rating<1900)。问n场之后rating的最大值为多少?


题解:


我的做法: 求n场后rating的最大值,那么判断出rating的初始值就可以了。 在div1和div2之间第一次变化时,我们可以根据ci得到变化前可能存在的rating范围(不超过100个),然后进行下面的比赛,对应rating变化,不符合后面要求的就删除,最后再回过头判断一下是否合理。注意全为div1或者div2的情况。 复杂度O(n*ci)

没想好细节,fst了,哭~~。


AC代码如下:


#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 1e9+10; 
const int maxn = 2e5+10;
struct node
{
	int c,d;
}a[maxn];
int ans[110]; 
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;++i)
			scanf("%d%d",&a[i].c,&a[i].d);
		int sign=0,k=0,flag=0;
		if(a[0].d==2)
			sign=1; 
		else 
			flag=1;
		int i;
		for(i=1;i<n;++i)//在div2和div1之间变化时,找到符合第一次变化前所有可能的情况 
		{
			if(a[i].d==2)
				sign=1;
			else 
				flag=1;
			if(a[i-1].d==2 && a[i].d==1)
			{
				for(int cnt=1900-a[i-1].c;cnt<1900;++cnt)
					ans[k++]=cnt;
				break;
			}
			if(a[i-1].d==1 && a[i].d==2)
			{
				for(int cnt=1900;cnt<=1899-a[i-1].c;++cnt)
					ans[k++]=cnt;
				break;
			}
		}
		for(i--;i<n;++i)//往后更新,并删除不符合后面情况的可能 
		{
			for(int j=0;j<k;++j)
			{
				if(ans[j]!=INF){
					if(ans[j]<1900 && a[i].d==1)
						ans[j]=INF;
					else if(ans[j]>=1900 && a[i].d==2)
						ans[j]=INF;
					else 
						ans[j]+=a[i].c;
				}
			}
		}
		if(!sign)//全部都是div1,最大值无上限 
				printf("Infinity\n");
		else {
			int res=INF;
			for(int i=0;i<k;++i)//检查得到的可存在解是否合法 
			{
				int cnt=ans[i];
				if(cnt==INF)
					continue;
				for(int j=n-1;j>=0;--j)
				{
					cnt-=a[j].c;
					if(cnt<1900 && a[j].d==1){
						ans[i]=INF;
						break;
					}
					if(cnt>=1900 && a[j].d==2){
						ans[i]=INF;
						break;
					}
				}
			}
			for(int i=0;i<k;++i)
			{
				if(ans[i]!=INF)
					res=ans[i];
			}
			if(flag==0)//特殊处理全是div2的情况 
			{
				int Max = 0;
				int temp = -1;
				int sum=0;
				for(int i=0;i<n;++i)
				{
					sum+=a[i].c;
					if(sum>Max)
					{
						Max=sum;
						temp=i;
					}
				}
				res=1899;
				if(temp==n-1)//div2一直涨分的情况,就保证倒数第二场结束分数为1899 
					res+=a[n-1].c;
				for(int i=temp+1;i<n;++i)//累积加分的最多场次加分到1899,那么后面无论怎么样都不会超过1899,符合一直在div2的情况 
					res+=a[i].c;
			}
			if(res==INF)//所有的情况都不合法 
				printf("Impossible\n"); 
			else 
				printf("%d\n",res);		
		}
	}
	return 0;
}


大佬们的做法:翻翻dalao们的代码,发现都差不多,十几行就能解决的思路。记下来


#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int mod = 1e9 + 7;
const int MX = 2e5 + 5;
const int INF = 0x3f3f3f3f;

int n;
PII S[MX];

int main(){
    // FIN;
    scanf("%d",&n);
    int L=-INF,R=INF;
    for(int i=1;i<=n;i++){
        scanf("%d%d",&S[i].first,&S[i].second);
    }

    int pre=0;
    for(int i=1;i<=n;i++){
        if(S[i].second==1) L=max(L,1900-pre);
        if(S[i].second==2) R=min(R,1899-pre);
        pre+=S[i].first;
    }
    if(L>R){
        printf("Impossible\n");
    }
    else if(L<=R&&R==INF){
        printf("Infinity\n");
    }else{
        printf("%d\n",R+pre);
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值