Rokua P1156 Garbage trap [backpack DP]

Source: Internet
Author: User

Title Description

Carmen-a Holsteins cow that farmer John Cherished dearly-has fallen into the "rubbish well." "Rubbish wells" are places where farmers throw rubbish, with a depth of D (2<=d<=100) feet.

Carmen tried to pile up the rubbish, and when the heap was as high as the well, she could escape out of the well. In addition, Carmen can maintain her life by eating some rubbish.

Every litter can be used to eat or stack, and to pile up rubbish does not take Carmen's time.

Let's say Carmen knew in advance what time t (0< t<=1000) each garbage dropped, and the height of each garbage heap H (1<=h<=25) and the time F (1<=f<=30) to eat the garbage to sustain life, Ask Carmen to escape the first time outside the well, assuming that Carmen currently has enough energy to last 10 hours, if Carmen did not eat within 10 hours, Carmen will starve to death.

Input/output format

Input format:

The first Acts 2 integers, D and g (1 <= g <=), G for the amount of garbage put into the well.

The second to the G+1 line consists of 3 integers: T (0 < T <= 1000), indicating the time that the garbage was dropped into the Well, F (1 <= F <= 30), indicating that the garbage could maintain the time of Carmen's life, and H (1 <= H <= 25), the garbage can Height to be padded.

Output format:

If Carmen can crawl out of the trap, output an integer indicating when the earliest can be crawled out, or how long the output of Carmen can survive.

Input and Output Sample input example # #:
20 45 4 99 3 212 6 1013 1 1
Sample # # of output:
13
Description

[Sample Description]

Carmen piled up the first rubbish she received: height=9;

Carmen eats the second litter she receives, extending her life from 10 hours to 13 hours;

Carmen piled up a 3rd litter, height=19;

Carmen piled up a 4th litter, height=20.

First think F[i][j] indicates the maximum remaining time of the first I height J

Encounter a garbage I can make to J have surplus on output a[i].t

Otherwise, choose the best from f[i][0]+a[i].t.

Note that the 0 moment Carmen is not dead, so initialize-1

main.cpp//luogu1156////Created by Candy on 10/11/2016.//copyright©2016 Candy. All rights reserved.//#include <iostream> #include <cstdio> #include <algorithm> #include <cstring    >using namespace Std;const int M=3005,n=105;inline int read () {char c=getchar (); int x=0,f=1; while (c< ' 0 ' | |    C> ' 9 ') {if (c== '-') F=-1;c=getchar ();}    while (c>= ' 0 ' &&c<= ' 9 ') {x=x*10+c-' 0 '; C=getchar ();} return x*f;}    int d,h,n,mp[n];struct data{int t,w,h; BOOL operator < (const data &r) Const{return t<r.t;}}    A[n];int F[n][m];bool dp () {memset (f,-1,sizeof (f));    f[0][0]=10;    a[0].t=0;            for (int i=1;i<=n;i++) for (int j=0;j<=h;j++) {int t=a[i].t-a[i-1].t;            if (f[i-1][j]-t>=0) F[I][J]=F[I-1][J]-T+A[I].W;                        if (j-a[i].h>=0&&f[i-1][j-a[i].h]-t>=0) F[i][j]=max (F[I][J],F[I-1][J-A[I].H]-T);            printf ("F%d%d%d\n", i,j,f[i][j]); IF (j>=d&&f[i][j]>=0) {printf ("%d\n", a[i].t); return true;} } return false;}    int main (int argc, const char * argv[]) {d=read (); N=read ();        for (int i=1;i<=n;i++) {a[i].t=read (); A[i].w=read (); A[i].h=read ();    H+=a[i].h;    } sort (a+1,a+1+n);        if (!DP ()) {int mx=10;        for (int i=n;i>=1;i--) if (f[i][0]>=0) Mx=max (mx,f[i][0]+a[i].t);            for (int i=1;i<=n;i++) {if (mx<a[i].t) break;        MX+=A[I].W;            } printf ("%d", MX); } return 0;}

There is another way of writing, f[i][j] means the longer time

By the way scrolling array + Brush table method to be faster

main.cpp//luogu1156////Created by Candy on 10/11/2016.//copyright©2016 Candy. All rights reserved.//#include <iostream> #include <cstdio> #include <algorithm> #include <cstring    >using namespace Std;const int N=105,m=3005;inline int read () {char c=getchar (); int x=0,f=1; while (c< ' 0 ' | |    C> ' 9 ') {if (c== '-') F=-1;c=getchar ();}    while (c>= ' 0 ' &&c<= ' 9 ') {x=x*10+c-' 0 '; C=getchar ();} return x*f;}    int d,h,n,mp[n];struct data{int t,w,h; BOOL operator < (const data &r) Const{return t<r.t;}}    A[n];int F[m];bool dp () {f[0]=10; for (int i=1;i<=n;i++) for (int j=d;j>=0;j--) if (f[j]>=a[i].t) {F[j+a[i].h]=max (f[j],f[j+a[i].h])            ;            F[J]+=A[I].W;        if (J+a[i].h>=d) {printf ("%d", a[i].t); return true;} } return false;}    int main (int argc, const char * argv[]) {d=read (); N=read ();        for (int i=1;i<=n;i++) {a[i].t=read (); A[i].w=read (); A[i].h=read ();    H+=a[i].h;   } Sort (a+1,a+1+n);        if (!DP ()) printf ("%d", f[0]); return 0;}

F[i] Indicates the maximum height of the time I J can also

Rokua P1156 Garbage trap [backpack DP]

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.