抄的动态规划代码:

#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...