跳到正文

题解

CF1203F2

yimuhua约 13 分钟
原文
文章信息与版本

文章信息

发布
2022年5月26日
收录
2026年9月1日
更新
2026年11月11日
篇幅
约 13 分钟
获赞
18
题目
CF1203F2

版本

  1. v1当前

题意:

nn 个项目,初始时评级为 rr。每个项目中有两个数值 aia_i 和 bib_i,aia_i 表示只有 r≥air\geq a_i 才能做这个项目,bib_i 表示完成这个项目后 rr 会加上 bib_i (可正可负)。求通过调整项目顺序最多可以完成多少项目。

思路:

贪心决定顺序,DP完成选择

贪心:

对于 bi≥0b_i \geq 0,显然可以按照 aia_i 从小到大排序,算出 cntcnt 和 rr;

对于 bi<0b_i < 0,则按照 ai+bia_i+b_i 从大到小排序

具体证明

可以将 ai+bia_i+b_i 看作做完此任务后的 rr 的下界来理解

DP:

方法一:

dpi,jdp_{i,j} 表示考虑完前 ii 个项目后评分为 jj 的最大任务完成数

枚举前面的任务,枚举能力值,满足条件则转移。

代码如下:

C++
#include <bits/stdc++.h>
using namespace std;
int n, r, cnt, ans, cnt1, cnt2, dp[105][60005];
struct node {
    int x, y;
}a[105], b[105];
bool cmp(node x, node y) {
    return x.x < y.x;
}
bool cmp2(node x, node y) {
    return x.x + x.y > y.x + y.y;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> r;
    for(int i = 1, x, y; i <= n; i++) {
        cin >> x >> y;
        if(y < 0)
            b[++cnt2] = (node){x, y};
        else
            a[++cnt1] = (node){x, y};
    }
    sort(a + 1, a + cnt1 + 1, cmp), sort(b + 1, b + cnt2 + 1, cmp2);
    for(int i = 1; i <= cnt1; i++)
        if(r >= a[i].x)
            r += a[i].y, cnt++;
        else
            break;
    for(int i = 1; i <= cnt2; i++)
        if(r >= max(b[i].x, abs(b[i].y))) {
            dp[i][r + b[i].y] = 1, ans = max(ans, 1);
            for(int j = i - 1; j; j--)
                for(int k = max(b[i].x, abs(b[i].y)); k < 60001; k++)
                    if(dp[j][k])
                        dp[i][k + b[i].y] = max(dp[i][k + b[i].y], dp[j][k] + 1), ans = max(ans, dp[i][k + b[i].y]);
        }
    cout << cnt + ans;
    return 0;
}

时间复杂度为 O(n×bi)O(n \times b_i)

方法二:

虽然方法一可以通过此题,但是我们不妨想想如何更快。

根据方法一可知,在评分确定时要最大化任务数,于是我们可以将第二维替换,在任务数确定时最大化评分

所以 dpi,jdp_{i,j} 表示前 ii 个任务完成任务数为 jj 时的最大评分

枚举任务和完成任务数,条件满足则转移。最后枚举 dpcnt2,idp_{cnt2,i},找到最大的 dpcnt2,i≥0dp_{cnt2,i}\geq 0 的 ii

代码如下:

C++
#include <bits/stdc++.h>
using namespace std;
int n, r, cnt, ans, cnt1, cnt2, dp[105][105];
struct node {
    int x, y;
}a[105], b[105];
bool cmp(node x, node y) {
    return x.x < y.x;
}
bool cmp2(node x, node y) {
    return x.x + x.y > y.x + y.y;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> r;
    for(int i = 1, x, y; i <= n; i++) {
        cin >> x >> y;
        if(y < 0)
            b[++cnt2] = (node){x, y};
        else
            a[++cnt1] = (node){x, y};
    }
    sort(a + 1, a + cnt1 + 1, cmp), sort(b + 1, b + cnt2 + 1, cmp2);
    for(int i = 1; i <= cnt1; i++)
        if(r >= a[i].x)
            r += a[i].y, cnt++;
        else
            break;
    for(int i = 1; i <= cnt2; i++)
        for(int j = 0; j <= i; j++)
            dp[i][j] = -1e9;
    dp[1][0] = r;
    if(b[1].x <= r && abs(b[1].y) <= r)
        dp[1][1] = r + b[1].y;
    for(int i = 1; i <= cnt2; i++)
        for(int j = 0; j <= i; j++) {
            if(b[i + 1].x <= dp[i][j])
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + b[i + 1].y);
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        }
    for(int i = 1; i <= cnt2; i++)
        if(dp[cnt2][i] >= 0)
            ans = max(ans, i);
    cout << cnt + ans;
    return 0;
}

时间复杂度为 O(n2)O(n^2)

方法三:

我们考虑舍弃 DP,转而用带反悔的贪心来完成选择

我们可以使用优先队列来维护在当前任务之前所有已选任务中 bib_i 最小的,也就是扣分最多的,在判断去除此任务后当前任务是否能完成

代码如下:

C++
#include <bits/stdc++.h>
using namespace std;
int n, r, cnt, ans, cnt1, cnt2;
struct node {
    int x, y;
    bool operator < (const node &b) const {
        return y > b.y;
    }
}a[105], b[105];
bool cmp(node x, node y) {
    return x.x < y.x;
}
bool cmp2(node x, node y) {
    return x.x + x.y > y.x + y.y;
}
priority_queue<node> pq;
int main() {
    ios::sync_with_stdio(0);
    cin >> n >> r;
    for(int i = 1, x, y; i <= n; i++) {
        cin >> x >> y;
        if(y < 0)
            b[++cnt2] = (node){x, y};
        else
            a[++cnt1] = (node){x, y};
    }
    sort(a + 1, a + cnt1 + 1, cmp), sort(b + 1, b + cnt2 + 1, cmp2);
    for(int i = 1; i <= cnt1; i++)
        if(r >= a[i].x)
            r += a[i].y, cnt++;
        else
            break;
    for(int i = 1; i <= cnt2; i++)
        if(r >= b[i].x && r + b[i].y >= 0)
            r += b[i].y, pq.push(b[i]), ans++;
        else if(!pq.empty() && pq.top().y < b[i].y) {
            r -= pq.top().y;
            if(r >= b[i].x)
                r += b[i].y, pq.pop(), pq.push(b[i]);
            else
                r += pq.top().y;
        }
    cout << cnt + ans;
    return 0;
}

在最后一份代码中,有这样一段:

C++
if(r >= b[i].x)
    r += b[i].y, pq.pop(), pq.push(b[i]);
else
    r += pq.top().y;

我们发现这个判断其实不需要,可以证明:

对于 a1,b1,a2,b2,a3,b3a_1,b_1,a_2,b_2,a_3,b_3

  • a1+b1≥a2+b2≥a3+b3a_1+b_1 \geq a_2+b_2 \geq a_3+b_3

  • r≥a1,r+b1≥a2,r+b1+b2<a3r \geq a_1,r+b_1 \geq a_2,r+b_1+b_2<a_3

  • b1≤b2,b1≤b3b_1\leq b_2,b_1\leq b_3

求证 r+b2≥a3r+b_2\geq a_3

证:

∵b1<b3\because b_1<b_3

∴a3+b3>a3+b1\therefore a_3+b_3>a_3+b_1

∵r+b1≥a2\because r+b_1\geq a_2

∴r+b1+b2≥a2+b2≥a3+b3≥a3+b1\therefore r+b_1+b_2 \geq a_2+b_2\geq a_3+b_3\geq a_3+b_1

∴r+b2≥a3\therefore r+b_2\geq a_3

对于 a1,b1,a2,b2,a3,b3,a4,b4a_1,b_1,a_2,b_2,a_3,b_3,a_4,b_4

  • a1+b1≥a2+b2≥a3+b3≥a4+b4a_1+b_1 \geq a_2+b_2 \geq a_3+b_3 \geq a_4+b_4

  • r≥a1,r+b1≥a2,r+b1+b2≥a3,r+b1+b2+b3<b4r \geq a_1,r+b_1 \geq a_2,r+b_1+b_2\geq a_3,r+b_1+b_2+b_3<b_4

  • b1≤b2,b1≤b3,b1≤b4b_1\leq b_2,b_1\leq b_3,b_1\leq b_4

求证 r+b2+b3≥a4r+b_2+b_3\geq a_4

证:

∵b1<b4\because b_1<b_4

∴a4+b4>a4+b1\therefore a_4+b_4>a_4+b_1

∵r+b1+b2≥a3\because r+b_1+b_2\geq a_3

∴r+b1+b2+b3≥a3+b3≥a4+b4≥a4+b1\therefore r+b_1+b_2+b_3\geq a_3+b_3\geq a_4+b_4 \geq a_4+b_1

∴r+b2+b3≥b4\therefore r+b_2+b_3\geq b_4

以此类推

最终代码:

C++
#include <bits/stdc++.h>
using namespace std;
int n, r, cnt, ans, cnt1, cnt2;
struct node {
    int x, y;
    bool operator < (const node &b) const {
        return y > b.y;
    }
}a[105], b[105];
bool cmp(node x, node y) {
    return x.x < y.x;
}
bool cmp2(node x, node y) {
    return x.x + x.y > y.x + y.y;
}
priority_queue<node> pq;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> r;
    for(int i = 1, x, y; i <= n; i++) {
        cin >> x >> y;
        if(y < 0)
            b[++cnt2] = (node){x, y};
        else
            a[++cnt1] = (node){x, y};
    }
    sort(a + 1, a + cnt1 + 1, cmp), sort(b + 1, b + cnt2 + 1, cmp2);
    for(int i = 1; i <= cnt1; i++)
        if(r >= a[i].x)
            r += a[i].y, cnt++;
        else
            break;
    for(int i = 1; i <= cnt2; i++)
        if(r >= b[i].x && r + b[i].y >= 0)
            r += b[i].y, pq.push(b[i]), ans++;
        else if(!pq.empty() && pq.top().y < b[i].y)
            r -= pq.top().y, r += b[i].y, pq.pop(), pq.push(b[i]);
    cout << cnt + ans;
    return 0;
}
原文
存档评论2 条抓取于