sdut 3903 CF(贪心+背包)

题意:在T分钟内完成n道题,每一道题花费时间c,每一道题完成时的得分为a-d*t,即完成这道题所给的初始分数减去完成这道题时所用的时间乘这道题每分钟减掉的分数,问在T分钟内能得到的最大分数是多少?

思路:首先假设有2道题,先做1,在做2,得到的分数为 a1 - d1 * c1 + a2 - (c1 + c2) * d2  先做2,在做1,得到的分数为a2 - d2 * c2 + a1 - (c1 + c2)  * d1 ,贪心的思想,那个顺序所得到的分数多,我们就采取哪种顺序,所以我们就按照这个原则排序就行了,对两个式子化简,只需对 -c1 * d2 和 -c2 * d1 排序就行了,又因为c越小,d越大这个表达式就越大,所以我们队d/c排序就行了,然后在用01背包得到最优解

#include <stdio.h>
#include <algorithm>

using namespace std;
struct node{
	int a,d,c;
	double vl;
}it[2002];
bool cmp(struct node a,struct node b){
	return a.vl > b.vl;
}
int dp[5005];

int main(void){
	int n,t;
	scanf("%d%d",&n,&t);
	for(int i = 0; i < n; i++){
		scanf("%d",&it[i].a);
	}
	for(int i = 0; i < n; i++){
		scanf("%d",&it[i].d);
	}
	for(int i = 0; i < n; i++){
		scanf("%d",&it[i].c);
	}
	for(int i = 0; i < n; i++){
		it[i].vl = it[i].d * 1.0 / it[i].c;
	}
	sort(it,it + n,cmp);
	int ans = 0;
	for(int i = 0; i < n; i++){
		for(int j = t; j >= it[i].c; j--){
			dp[j] = max(dp[j],dp[j - it[i].c] + it[i].a - it[i].d * j);
			ans = max(ans,dp[j]);
		}
	}
	printf("%d\n",ans);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值