The problem of Ants (iii)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.