题解:P13957 [ICPC 2023 Nanjing R] 背包
简化题意:
有
思路:
对于如何利用这
- 选取
k 个美丽度最大的。 - 选取
k 个价格最大的。
但显然对于思路 1,若我们只考虑美丽度而没有考虑价格,可能会导致我们只能得到
代码
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
const int N = 5e3 + 10;
const int M = 1e4 + 10;
int n,W,k;
int f[M];
struct Node{
int w,v;
}a[N];
int ans;
bool cmp(Node x,Node y){return x.w < y.w;}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> W >> k;
for(int i = 1;i <= n;i ++){
int w,v;
cin >> w >> v;
a[i] = {w,v};
}
sort(a + 1,a + 1 + n,cmp);
for(int i = 1;i <= n;i ++){
for(int j = W;j >= a[i].w;j --){
f[j] = max(f[j],f[j - a[i].w] + a[i].v);
}
priority_queue<int>q;//优先队列自动排序,也可以使用数组sort排序
for(int p = i + 1;p <= n;p ++)q.push(a[p].v);
int now = f[W];//花钱前i个物品的最大美丽度
int m = min((int)q.size(),k);
while(m --){
now += q.top();
q.pop();
}
ans = max(now,ans);
}
cout << ans;
return 0;
}