Codeforces750C-New Year and Rating(二分+模拟)

题目链接

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值