题目链接
http://codeforces.com/contest/750/problem/C
思路
二分初始分数x,对初始分数x进行合法性判断即可
注意边界条件的判断,以及当时二分边界写跪了被hack了。。二分左端点可以是负值
代码
#include <bits/stdc++.h>
using namespace std;
inline int in() {int x; scanf("%d", &x); return x;}
#define pr(x) {cout << #x << ' ' << x << endl;}
#define LL long long
const int maxn = 200000 + 5;
LL c[maxn];
int d[maxn];
int n;
LL res = -1e15;
//1 to0 large
//-1 to0 small
int judge(LL x) {
for (int i = 1; i <= n; i++) {
if (x >= 1900) {
if (d[i] == 2) return 1;
else x += c[i];
} else {
if (d[i] == 1) return -1;
else x += c[i];
}
}
res = x;
return 0;
}
int main() {
n = in();
for (int i = 1; i <= n; i++) scanf("%I64d%d", &c[i], &d[i]);
LL L = -INT_MAX, R = INT_MAX, M = (L + R) / 2;
bool flag = true;
while (L < R) {
if (L == R - 1) {
if (judge(R) == 0) M = R;
else if (judge(L) == 0) M = L;
else flag = false;
break;
} else {
M = (L + R) / 2;
int f = judge(M);
if (f == -1) L = M;
else if (f == 1) R = M;
else {
L = M;
}
}
}
if (M == INT_MAX) {
cout << "Infinity" << endl;
return 0;
}
if (!flag) {
if (res != -1e15) cout << res << endl;
else cout << "Impossible" << endl;
}
else {
cout << res << endl;
}
return 0;
}