HDU Max Sum plus Plus

Source: Internet
Author: User

With a long long unexpectedly time out

Ideas refer to Kuangbin's

Because N has 1000000, you can use a scrolling array

Then dp[i][j] means selecting the number of I, the maximum value when divided into J Group

And here's a more unique idea.

The general idea is that the number of choice or not to choose these two states, and here do not need to choose this state, the state can be selected by the <i state, that is, Max (dp[i-1][j-1],dp[i-2][j-1]......dp[j-1][j-1)

Also note that there is no point in i<j here, so I did not calculate it

Have done some DP of the topic, but are some difficult, the foundation did not play well, come back to do some basic dp//actually do DP of the topic is best not to see the implementation of the details of others, only to see the idea can be, one of the code of others to understand too difficult, and other people's things eventually others, Only the things that are written by yourself//and the DP sometimes if the loop starts at 1, there will be some values that are not meaningful in the actual situation will be calculated, then we can add some special, for this situation do not consider//with a long long unexpectedly time out, is drunk # include<stdio.h> #include <algorithm> #include <string.h>using namespace std; #define N 1000005# Define ll Long Longint Dp[n],maxn[2][n];//maxn[0][i] The maximum value of the number of previous I in the current loop int A[n];int main () {#ifndef Online_judgefreopen (" In.txt "," R ", stdin), #endifint m,n;while (scanf ("%d%d ", &m,&n)!=eof) {memset (dp,0,sizeof (DP)); memset (maxn,0, sizeof (MAXN)); int *x=maxn[0],*y=maxn[1];//x is the previous layer m, Y is the current for (int i=1;i<=n;i++) scanf ("%d", &a[i]); int sum=0; for (int i=1;i<=m;i++) {sum+=a[i];//The sum of the pre-I value and for (int j=i;j<=n;j++) {//j<i has no practical meaning if (i==j) {dp[j]=sum;y[j]=dp[j];} else {Dp[j]=max (dp[j-1]+a[j],x[j-1]+a[j]);//For the first layer of the loop, Dp[j] is either not taken or taken, so the initial x[j-1] initial value is assigned 0 y[j]=max (dp[j],y[j-1]);}} Swap (x, y),/*for (int j=1;j<=n;j++) {printf ("%d:%d", J,dp[j]);} printf ("\ n"); for (int j=1;j<=n;j++) {printf ("%d:%d ", J,y[j]);} printf ("\ n");//For debugging DP programs preferably in a table-type debugging, tracking execution is basically nothing to use */}printf ("%d\n", X[n]);}}


HDU Max Sum plus Plus

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.