Good Bye 2016 C. New Year and Rating 不等式+贪心、数学
来源:互联网 发布:淘宝淘友在哪儿看 编辑:程序博客网 时间:2024/06/07 05:11
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 15 28 2
1907
257 122 2
Impossible
1-5 1
Infinity
427 213 1-50 18 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.
Source
Good Bye 2016
My Solution
题意:rating == 1900是区分各个分组的界限,给出给出n个ci 和 di,表示rating上涨了ci分(ci正为涨分负为掉分) 且当场比赛是在di分区大的,不知道起始的rating,只给出了n场连续的比赛涨分情况和所在分组,求出n场比赛之后可能的rating的最大值。
不等式+贪心、数学
很棒的题,
l = -INF, r = INF,l 表示当前rating可能值的最小值,r表示当前rating可能值的最大值。
如果当前分组是div2则r = min(r, 1899),,
如果当前是div2,上一次是div1,则r = min(r, 1899);, l = max(l, 1900);然后l += v,r += v,
如果当前是div1,上一次是div2,则r = min(r, 1899 + prev);,l = max(l, 1900 + prev);,然后l += v,r += v,
其它情况只做普通刷新,且当l > r 时 ans = -1;
当前所在的分组表示的是上一次比赛结束后,rating所在的分组,
具体见源码 ^_^
复杂度 O(n)
#include <iostream>#include <cstdio>using namespace std;typedef long long LL;const int maxn = 2e5 + 8;const int INF = 1e8;int main(){ #ifdef LOCAL freopen("c.txt", "r", stdin); //freopen("c.out", "w", stdout); int T = 4; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); int n, v, d, l = -INF, r = INF, pred = -1, prev = -1; cin >> n; int ans = 0; for(int i = 0; i < n; i++){ //cout << l << " " << r << endl; cin >> v >> d; if(i == 0){ if(d == 2){ r = 1899; r += v; } else if(d == 1){ l = 1900; l += v; } pred = d; prev = v; continue; } if(d == 1){ if(r < 1900){ ans = -1; //break; } else{ if(pred == 2){ r = min(r, 1899 + prev); r += v; l = max(l, 1900);// l += v; if(l > r){ ans = -1; } } else{ if(r != INF) {r += v;} if(l != -INF) {l = max(1900, l); l += v;} } } } else if(d == 2){ if(l >= 1900){ ans = -1; //break; } else{ if(pred == 1){ r = min(r, 1899);//cout <<r << " pred " << pred << " " << v << " " << d << " ?"; r += v; l = max(l, 1900 + prev); l += v; if(l > r){ ans = -1; } } else{ if(r != INF) {r = min(r, 1899); r += v; } if(l != -INF) l += v; } } } if(l > r){ ans = -1; } pred = d; prev = v; } if(ans == -1) cout << "Impossible" << endl; else if(r == INF) cout << "Infinity" << endl; else cout << r << endl; #ifdef LOCAL cout << endl; } #endif // LOCAL return 0;}
Thank you!
------from ProLights
- Good Bye 2016 C. New Year and Rating 不等式+贪心、数学
- codeforces Good Bye 2016-C New Year and Rating(二分)
- Good Bye 2016 C. New Year and Rating(模拟)
- Good Bye 2016 C. New Year and Rating思维
- Codeforces Good Bye 2016 C. New Year and Rating(模拟)
- CodeForces Good Bye 2016 :C New Year and Rating、D New Year and Fireworks、E New Year and Old Subsequ
- CF Good Bye 2016 C New Year and Rating 模拟乱搞
- Codeforces Good Bye 2016 C.New Year and Rating//qduoj 三亿河老师的上分梦想
- Good Bye 2014 C. New Year Book Reading 贪心
- GoodBye 2016 C. New Year and Rating 详解(维护不等式)
- Codeforces Good Bye 2015 C. New Year and Domino (预处理)
- Good Bye 2015 C New Year and Domino(dp)
- CF Good Bye 2015 C- New Year and Domino(CF611C)
- codeforces Good Bye 2015 C - New Year and Domino
- codeforces Good Bye 2016-B New Year and North Pole
- codeforces Good Bye 2016-D New Year and Fireworks(dfs)
- Good Bye 2016D. New Year and Fireworks(dfs)
- CF Good Bye 2015 E. New Year and Three Musketeers(贪心+枚举)
- 401. Binary Watch*
- removeAttr() 和 removeProp() 以及 removeClass("类名")和removeClass()的区别
- Android 6.0之后的动态权限
- 欢迎使用CSDN-markdown编辑器
- Android中EventBus3.0的使用
- Good Bye 2016 C. New Year and Rating 不等式+贪心、数学
- 为Hexo NexT主题添加ShareSDK的分享
- Java基础背景常识
- 89. Gray Code**
- 问题十三:怎么用ray tracing画个球
- 阻塞队列实现生产者消费者模式
- html5--canvas--众神眷恋的幻想乡(STG)
- 多线程下载|Axel命令
- 问题十四:怎么可视化球的法向量