题意:在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;
}