The Highest Mark(01背包)

时间:2021-08-29 18:41:47

The Highest Mark

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 247    Accepted Submission(s): 101


Problem Description
The SDOI in  2045 is far from what it was been 30 years ago. Each competition has t minutes and n problems.

The ith problem with the original mark of Ai(Ai106),and it decreases Bi by each minute. It is guaranteed that it does not go to minus when the competition ends. For example someone solves the ith problem after x minutes of the competition beginning. He/She will get AiBix marks.

If someone solves a problem on x minute. He/She will begin to solve the next problem on x+1 minute.

dxy who attend this competition with excellent strength, can measure the time of solving each problem exactly.He will spend Ci(Cit) 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(T10) in the first line for the number of testcases.(the number of testcases with n>200 is no more than 5)

For each testcase, there are two integers in the first line n(1n1000) and t(1t3000) for the number of problems and the time limitation of this competition.

There are n lines followed and three positive integers each line Ai,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 2 and then solve problem 1 he will get 88 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 2
 

 

Sample Output
88
题解:01背包问题。。。不过要预处理下,按照分数流失度从大到小排;
代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<queue>
 5 #define MIN(x,y)(x<y?x:y)
 6 #define MAX(x,y)(x>y?x:y)
 7 const int INF=0x3f3f3f3f;
 8 using namespace std;
 9 struct Node{
10     int a,b,c;
11     void in(){
12         scanf("%d%d%d",&a,&b,&c);
13     }
14     friend bool operator <(Node a,Node b){
15         return a.b*b.c>a.c*b.b;
16     }
17 };
18 Node dt[1010];
19 int bag[3010];
20 int main(){
21     int T,n,t;
22     scanf("%d",&T);
23     while(T--){
24         scanf("%d%d",&n,&t);
25         for(int i=0;i<n;i++){
26             dt[i].in();
27         }
28         sort(dt,dt+n);
29         memset(bag,0,sizeof(bag));
30         for(int i=0;i<n;i++){
31             for(int j=t;j>=dt[i].c;j--){
32                 bag[j]=MAX(bag[j],bag[j-dt[i].c]+dt[i].a-j*dt[i].b);
33             }
34         }
35         int max=0;
36         for(int i=0;i<=t;i++)max=MAX(max,bag[i]);
37         printf("%d\n",max);
38     }
39     return 0;
40 }

明知道暴力肯定超时,还是写了下;

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<queue>
 5 #define MIN(x,y)(x<y?x:y)
 6 #define MAX(x,y)(x>y?x:y)
 7 const int INF=0x3f3f3f3f;
 8 using namespace std;
 9 int n,t,ans;
10 int vis[1010];
11 struct Node{
12     int a,b,c;
13 };
14 Node dt[1010];
15 void dfs(int time,int score){
16     if(time>t)return;
17     ans=MAX(ans,score);
18     for(int i=0;i<n;i++){
19         if(vis[i])continue;
20         vis[i]=1;
21         dfs(time+dt[i].c,score+dt[i].a-dt[i].b*(time+dt[i].c));
22         vis[i]=0;
23     }
24 }
25 int main(){
26     int T;
27     scanf("%d",&T);
28     while(T--){
29         scanf("%d%d",&n,&t);
30         for(int i=0;i<n;i++){
31             scanf("%d%d%d",&dt[i].a,&dt[i].b,&dt[i].c);
32         }
33         memset(vis,0,sizeof(vis));
34         ans=0;
35         dfs(0,0);
36         printf("%d\n",ans);
37     }
38     return 0;
39 }