[CF1203F1] Complete the Projects

[CF1203F1] Complete the Projects - 临项交换排序

Description

打第 i 场比赛需要 ai 的 rating,打完第 i 场比赛后 rating change 是 bi,rating 必须非负。求是否存在顺序能完成所有项目。(n le 100, r le 30000, |b_i| le 300)

Solution

临项交换排序

对于 bi > 0 的部分,显然按 ai 排序

否则,按 ai+bi 排序即可

假如 1,2,初态为 r,此时要求 r>=a1, r>=a2-b1,若交换,则 r>=a2, r>=a1-b2,于是我们希望 max(a1,a2-b1)<=max(a2,a1-b2)
于是 a2+b2>=a1+b1

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1000005;

int n, r;
vector<pair<int, int>> a;

bool cmp(const pair<int, int> &lhs, const pair<int, int> &rhs)
{
    if (lhs.second >= 0 && rhs.second >= 0)
        return lhs.first < rhs.first;
    if (lhs.second < 0 && rhs.second < 0)
        return lhs.first + lhs.second > rhs.first + rhs.second;
    return lhs.second > rhs.second;
}

signed main()
{
    ios::sync_with_stdio(false);

    cin >> n >> r;
    for (int i = 1; i <= n; i++)
    {
        int x, y;
        cin >> x >> y;
        a.push_back({x, y});
    }
    sort(a.begin(), a.end(), cmp);

    for (int i = 0; i < n; i++)
    {
        int lim = a[i].first;
        int delta = a[i].second;
        if (r < lim)
        {
            cout << "NO";
            return 0;
        }
        r += delta;
        if (r < 0)
        {
            cout << "NO";
            return 0;
        }
    }
    cout << "YES" << endl;
}
原文地址:https://www.cnblogs.com/mollnn/p/14368014.html