CF1203F2
题意:
方法二:
虽然方法一可以通过此题,但是我们不妨想想如何更快。
根据方法一可知,在评分确定时要最大化任务数,于是我们可以将第二维替换,在任务数确定时最大化评分
所以
枚举任务和完成任务数,条件满足则转移。最后枚举
代码如下:
#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;
}
时间复杂度为
方法三:
我们考虑舍弃 DP,转而用带反悔的贪心来完成选择
我们可以使用优先队列来维护在当前任务之前所有已选任务中
代码如下:
#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 \geq a_2+b_2 \geq a_3+b_3 -
r \geq a_1,r+b_1 \geq a_2,r+b_1+b_2<a_3 -
b_1\leq b_2,b_1\leq b_3
求证
证:
对于
-
a_1+b_1 \geq a_2+b_2 \geq a_3+b_3 \geq a_4+b_4 -
r \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 -
b_1\leq b_2,b_1\leq b_3,b_1\leq 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;
}