the problem of ants ( III)
time limit: - ms | Memory Limit: 65535 KB
Difficulty: 4
Describe
The ants have finally moved as much of the ingredients home as possible, and now the chef program has begun.
It is known that there are a total of n Ingredients, each of which has a tasty Ai and freshness Bi , and if the ant cooks the sample I -like ingredients at T -moment, it gets Ai-t*bi 's delicious index, of course, cooking with the first ingredient is going to take Ci time.
As is known to all, ants are not very good at cooking, so he needs you to design a cooking plan to make the most delicious index done in time T.
Input
There are multiple sets of test data.
The first line is two positive integers, which indicates the cooking time of the ants. T and the number of ingredients N . (n<=50, 1<=t<=100000).
Next N rows, each line has three numbers, Ai,bi,ci . Represents the delicacy, freshness, and time spent cooking with the ingredients, respectively. (0<ai,bi,ci<=100000).
Output
Output a number indicating the maximum taste index
Sample input
6 1
200 5 1
Sample output
195
Ideas:
If not b[i] This attribute is the obvious 01 knapsack problem.
Now consider the adjacent two items x, Y. Assuming that the time is now spent on p, then the first x, y cost is listed separately:
a[x]-(P+c[x]) *b[x]+a[y]-(P+c[x]+c[y]) *b[y]①
a[y]-(P+c[y]) *b[y]+a[x]-(p+c[y]+c[x]) *b[x]②
For these two formulas degenerate, the condition of getting ①>② is c[x]*b[y]<c[y]*b[x].
It is found that the cost of x before Y is always better as long as the item pair (x, y) satisfies this condition.
So it can be sorted according to this condition, then the simple 01 backpack.
AC Code:
<pre name= "code" class= "HTML" > #include <stdio.h> #include <string.h> #include <stdlib.h>long Long d[100005];struct Node{long long a,b,c;} Num[60];long Long Max (long long X,long long y) {return x>y?x:y;} int cmp (const void *x,const void *y) {return (* (struct node *) x). c* (* (struct node *) y). B (* (struct node *) x). b* (* (struct NO de *) y). C;} int main () {Long Long t,n,i,j;struct node S;while (~scanf ("%lld%lld", &t,&n)) {for (i=0;i<n;i++) {scanf ("%lld% Lld%lld ", &num[i].a,&num[i].b,&num[i].c);} Qsort (Num,n,sizeof (num[0]), CMP), memset (d,0,sizeof (d)); for (i=0;i<n;i++) {for (j=t;j>=num[i].c;j--) {// Knapsack problem train of thought to obtain the optimal value D[j]=max (D[J],D[J-NUM[I].C]+NUM[I].A-J*NUM[I].B);}} for (i=1;i<=t;i++) D[0]=max (D[0],d[i]);p rintf ("%lld\n", D[0]);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The problem of Ants (iii)