参看资料:
https://blog.csdn.net/rain722/article/details/75418642
题目:
The SDOI in 20452045 is far from what it was been 3030 years ago. Each competition has ttminutes and nn problems.
The ithith problem with the original mark of Ai(Ai≤106)Ai(Ai≤106),and it decreases BiBi by each minute. It is guaranteed that it does not go to minus when the competition ends. For example someone solves the ithith problem after xx minutes of the competition beginning. He/She will get Ai−Bi∗xAi−Bi∗x marks.
If someone solves a problem on xx minute. He/She will begin to solve the next problem on x+1x+1 minute.
dxy who attend this competition with excellent strength, can measure the time of solving each problem exactly.He will spend Ci(Ci≤t)Ci(Ci≤t) minutes to solve the ith problem. It is because he is so godlike that he can solve every problem of this competition. But to the limitation of time, it's probable he cannot solve every problem in this competition. He wanted to arrange the order of solving problems to get the highest mark in this competition.Input
There is an positive integer T(T≤10)T(T≤10) in the first line for the number of testcases.(the number of testcases with n>200n>200 is no more than 55)
For each testcase, there are two integers in the first line n(1≤n≤1000)n(1≤n≤1000) and t(1≤t≤3000)t(1≤t≤3000) for the number of problems and the time limitation of this competition.
There are nn lines followed and three positive integers each line Ai,Bi,CiAi,Bi,Ci. For the original mark,the mark decreasing per minute and the time dxy of solving this problem will spend.
Hint:
First to solve problem 22 and then solve problem 11 he will get 8888 marks. Higher than any other order.Output
For each testcase output a line for an integer, for the highest mark dxy will get in this competition.
Sample Input
1 4 10 110 5 9 30 2 1 80 4 8 50 3 2Sample Output
88
题目大意:
给定时间 t ;有n道题目,对于题目i,有一个初始得分Ai,有一个每分钟掉分值 Bi,即在第x分 解出题目 i ,则可以得到的分数为Ai - Bi*x ; 现在有一个人,知道自己完成每一道题需要的时间,现在求合理安排做题顺序后该选手可以得到的最大分数。
解题思路:
一眼就明白了背包背在哪里,时间;然后贪心贪在哪里,总得分最大,当时没有深想,dp不就是为了解决贪心目光短浅的问题吗?但是这个先后规则,,怎么弄,,出于对dp的敬畏还有做不出题的难受,跳了过去。
挺同学讲了讲,看了看题解,慢慢明白了这个题优先选择的方式:
给出的题哪个先做没有先后顺序,那么在动态规划的时候,必然有前后顺序,所以要么就是状态压缩,要么就是之前就把这些题目排序了,使得后面是有序的。状压不用考虑了因为数据太大,所以我们考虑要如何排序。
现在有A1,B1,C1和A2,B2,C2这两道题,如果先做1再做2的得分是A1-B1*C1+A2-B2*(C1+C2),如果先做2在做1的得分是A2-B2*C2+A1-B1*(C1+C2),令先做1再做2的得分更高些,那么有A1-B1*C1+A2-B2*(C1+C2) >= A2-B2*C2+A1-B1*(C1+C2),解得B1*C2>=B2*C1【化简后】
所以,只要B1*C2>=B2*C1,那么先做题1再做题2的分数就会更高。【给出选择规则,至于最终的最高分是多少,交给dp来做】
所以,我们只需要根据这个来排序,再做dp,就能得到答案了。
实现代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
struct Ploblem
{
int a,b,c;
bool operator < (const Ploblem &other) const //排序规则
{
return b*other.c > other.b*c;
}
}p[maxn];
int dp[3005];
int main(){
int T, n, t;
scanf("%d", &T);
while(T--){
scanf("%d%d",&n,&t);
for(int i=1;i<=n;i++) //输入
scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].c);
memset(dp, 0, sizeof(dp)); //初始化
sort(p+1, p+n+1); //排序,结构体内有排序规则
int ans=0;
for(int i=1;i<=n;i++)
for(int j=t;j>=p[i].c;j--){
dp[j]=max(dp[j],dp[j-p[i].c]+(p[i].a-p[i].b * j));
ans = max(ans, dp[j]);
}
printf("%d\n", ans);
}
return 0;
}