Single-line DP review hdu1087

Source: Internet
Author: User

I wrote the idea that every DP "I" is the maximum value of the former DP "I"

DP "I" is equal to all previous DP "0 ... I-1 "Maximum value plus DP" I "

The maximum value is an intermediate variable

The most desirable condition is that the value of the sequence is incremented, that is, the a[i]>a[front]

#include <cstdio> #include <algorithm> #include <cstring>using namespace std;typedef long LL; LL a[1000+100]; LL Dp[1100];int Main () {    int n;    while (scanf ("%d", &n), n!=0) {        LL maxx=-4294967296;       for (int i=0;i<n;i++) scanf ("%lld", &a[i]);       for (int i=0;i<n;i++) {        LL x=-4294967296;        for (int j=0;j<i;j++)            if (a[i]>a[j]&&dp[j]>x) X=max (dp[j],x);        if (x = = -4294967296) x=0;        Dp[i]=x+a[i];        if (Dp[i] > Maxx) maxx=dp[i];       }    printf ("%lld\n", Maxx);    }    return 0;}

Read someone else's code, that is, I wrote the x middle variable into b[i]

B "I" is useless for DP "I" and can be used to store intermediate values

It saves a bit of code, and the principle is the same.

#include <cstdio> #include <algorithm> #include <cstring>using namespace Std;int a[1100],b[1100];int Main () {    int n;    while (scanf ("%d", &n) &&n!=0) {        memset (b,0,sizeof (b));        int maxx=-4294967296;        for (int i=0;i<n;i++) scanf ("%d", &a[i]);        for (int i=0;i<n;i++) {            b[i]=a[i];                for (int j=0;j<i;j++)            {                if (A[i]>a[j]&&b[i]<a[i]+b[j]) b[i]=b[j]+a[i];                if (B[i]>maxx) maxx=b[i];            }        }        printf ("%d\n", Maxx);    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Single-line DP review hdu1087

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.