CF1203F2

· · 题解

题意:

# 思路: 贪心决定顺序,**DP**完成选择 ## 贪心: 对于 $b_i \geq 0$,显然可以按照 $a_i$ 从小到大排序,算出 $cnt$ 和 $r$; 对于 $b_i < 0$,则按照 $a_i+b_i$ 从大到小排序 [具体证明](https://www.luogu.com.cn/blog/qcwlmqy/solution-cf1203f1) 可以将 $a_i+b_i$ 看作做完此任务后的 $r$ 的下界来理解 ## DP: ### 方法一: $dp_{i,j}$ 表示考虑完前 $i$ 个项目后评分为 $j$ 的最大任务完成数 枚举前面的任务,枚举能力值,满足条件则转移。 代码如下: ```cpp #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 \times b_i)

方法二:

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

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

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

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

代码如下:

#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(n^2)

方法三:

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

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

代码如下:

#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;
}

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

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

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

对于 a_1,b_1,a_2,b_2,a_3,b_3

求证 r+b_2\geq a_3

证:

\because b_1<b_3 \therefore a_3+b_3>a_3+b_1 \because r+b_1\geq a_2 \therefore r+b_1+b_2 \geq a_2+b_2\geq a_3+b_3\geq a_3+b_1 \therefore r+b_2\geq a_3

对于 a_1,b_1,a_2,b_2,a_3,b_3,a_4,b_4

求证 r+b_2+b_3\geq a_4

证:

\because b_1<b_4 \therefore a_4+b_4>a_4+b_1 \because r+b_1+b_2\geq a_3 \therefore r+b_1+b_2+b_3\geq a_3+b_3\geq a_4+b_4 \geq a_4+b_1 \therefore r+b_2+b_3\geq b_4

以此类推

最终代码:

#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;
}