SSL 2549 JZOJ 1756 得分#01背包,贪心#

比赛


题目

求按照一定的顺序得到的最大得分。


分析

普通的01背包是不可以的,因为它有顺序,所以用贪心的思想,把顺序确定出来,再用01背包。
状态转移方程: f [ j ] = max ⁡ ( f [ j ] , f [ j − w [ i ] ] + c [ i ] ∗ j ) f[j]=\max(f[j],f[j-w[i]]+c[i]*j) f[j]=max(f[j],f[jw[i]]+c[i]j)


代码

#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;
struct sh{int w,c;}sc[3001]; 
int n,m,ans,f[10001];
int in(){
	int ans=0; char c=getchar();
	while (!isdigit(c)) c=getchar();
	while (isdigit(c)) ans=ans*10+c-48,c=getchar();
	return ans;
}
bool cmp(sh x,sh y){return x.w*y.c>x.c*y.w;}//以乘积进行贪心
int main(){
	n=in(); m=in();
	for (int i=1;i<=n;i++) sc[i].w=in(),sc[i].c=in();
	stable_sort(sc+1,sc+1+n,cmp);
	for (int i=1;i<=n;i++)
	for (int j=m;j>=sc[i].w;j--){
		f[j]=max(f[j],f[j-sc[i].w]+sc[i].c*j);//背包
		if (i==n) ans=max(ans,f[j]);
	}
	printf("%d",ans); return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值