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