题意:
个项目,初始时评级为 。每个项目中有两个数值 和 , 表示只有 才能做这个项目, 表示完成这个项目后 会加上 (可正可负)。求通过调整项目顺序最多可以完成多少项目。
思路:
贪心决定顺序,DP完成选择
贪心:
对于 ,显然可以按照 从小到大排序,算出 和 ;
对于 ,则按照 从大到小排序
可以将 看作做完此任务后的 的下界来理解
DP:
方法一:
表示考虑完前 个项目后评分为 的最大任务完成数
枚举前面的任务,枚举能力值,满足条件则转移。
代码如下:
#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;
}时间复杂度为
方法二:
虽然方法一可以通过此题,但是我们不妨想想如何更快。
根据方法一可知,在评分确定时要最大化任务数,于是我们可以将第二维替换,在任务数确定时最大化评分
所以 表示前 个任务完成任务数为 时的最大评分
枚举任务和完成任务数,条件满足则转移。最后枚举 ,找到最大的 的
代码如下:
#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;我们发现这个判断其实不需要,可以证明:
对于
求证
证:
对于
求证
证:
以此类推
最终代码:
#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;
}