Good Bye 2016 C. New Year and Rating
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ncontests.
Sample Input
3
-7 1
5 2
8 2
2
57 1
22 2
1
-5 1
4
27 2
13 1
-50 1
8 2
Sample Output
1907
Impossible
Infinity
1897
Note
思路
题解:
二分查找出符合题意的值,判断最后判断二分出来的结果是不是正确的。
#include<bits/stdc++.h> using namespace std; const int maxn = 200005; const int Left = -20000000; const int Right = 20001900; struct Node{ int val,type; }node[maxn]; int n; bool OK(int x) { for (int i = 0;i < n;i++) { if (node[i].type == 1) { if (x < 1900) return true; x += node[i].val; } else { if (x > 1899) return false; x += node[i].val; } } return true; } int main() { //freopen("input.txt","r",stdin); scanf("%d",&n); for (int i = 0;i < n;i++) scanf("%d%d",&node[i].val,&node[i].type); int left = Left,right = Right + 300; while (left < right - 1) { int mid = left + ((right-left)>>1); if (OK(mid)) { left = mid; } else { right = mid; } } int val = left,sum = left; for (int i = 0;i < n;i++) { if (node[i].type == 1) { if (sum < 1900) { printf("Impossible\n"); return 0; } else sum += node[i].val; } else { if (sum > 1899) { printf("Impossible\n"); return 0; } else sum += node[i].val; } } if (val >= Right) printf("Infinity\n"); else printf("%d\n",sum); return 0; }
以上是关于Good Bye 2016 C. New Year and Rating的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Good Bye 2016 E. New Year and Old Subsequence
Good Bye 2015 F - New Year and Cleaning
Good Bye 2014 E - New Year Domino 单调栈+倍增
Codeforces Good Bye 2017 B. New Year and Buggy Bot 枚举全排列模拟