- C++
明天考CSP了 请假在家临时抱佛脚一下
- 2024-10-25 9:10:49 @
抄的动态规划代码:
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
struct score {
int value,weight;
};
vector <score> sell;
int n,w;
bool cmp(score x, score y) {
return x.value * x.weight > y.value * y.weight;
}
int max_sell() {
vector <long long> dp(w + 1,0);
for(int i = 0; i < sell.size(); ++i) {
for(int t = w; t >= sell[i].weight; --t) {
dp[t] = max(dp[t], dp[t - sell[i].weight] + sell[i].value);
}
}
return dp[w];
}
int main() {
scanf("%d%d",&n,&w);
for(int i = 0; i < n; ++i) {
score r;
scanf("%d%d",&r.value,&r.weight);
sell.push_back(r);
}
printf("%d",max_sell());
return 0;
}
0 comments
No comments so far...