NEUQ OJ 1486] [Dynamic Programming ★★ Max Sum Plus Plus. [One-dimensional array, memory optimization]

Copyright: original article, please do not reprint without permission https://blog.csdn.net/DanBo_C/article/details/89976922

Dynamic Programming [★★] Max Sum Plus Plus.

Title Description
give you an array of length n (1 <= n <= 1000000 ), an array of n elements S 1, S 2, S 3 , S 4 ... S n (-32768 ≤ S x ≤ 32767)
we define function sum (i, j) = S i + ... + S j (1 ≤ i ≤ j ≤ n)
now give you an integer m (0 <m <= n ) you need to find y = sum (i 1, j 1) + sum (i 2, j 2) + sum (i 3, the maximum value of j 3) + ... + sum ( im, jm) is, (ix ≤ iy ≤ jx or ix ≤ jy ≤ jx is not allowed )

Input Description
plurality of sets of data, for each set of data, only one row, two input numbers m and n, then n number following the input S 1, S 2, S 3 ... S n

Output Description
For each data row for each output, each output of the maximum value of y represents a number of
sample input
. 1 2. 3. 3. 1
2. 6. 3 -2 -2 -1. 4. 3
sample output
. 6
. 8

Problem-solving ideas

Maximum subsequence problem and it
generally means that, you bunch number, m segments removed, such that m and the maximum segment

Do not look at the subject, then, beginning no clear thoughts. I saw none. . .

Ok,,

  1. The situation is mapped to a two-dimensional array. The first column is the number of the array, where the first line segment is made available

  2. This matrix process simulation was able to calculate the final result. Simulation of the most important is the transfer equation, and that is the decision-making process of
    decision-making is divided into two
    - the first increase in the number of j j-1 was the first number after paragraph
    - the first of the j number of new paragraphs
    decision-making equation:

    d[n] = max(d[n] , max( d[j-1] , temp ) + a[j] );

    It written separately

    temp = max(d[j-1],temp) + a[j];
    d[n] = max(d[n],temp);

  3. Problem-solving

    1. Receiving the number, the array initialization dp

      for(int i=1;i<=n;i++)
      cin>>a[i];
      memset(dp,0,sizeof (dp) );

    2. Simulation, traverse field

      for(int i=1;i<=m;i++)

    3. Before i have to find items and (this is a period of time each) to obtain the decision 2

      int temp=0;
      for(int k =1;k<=i;k++)
      temp += a[k];
      ans = temp;

    4. for(int j = i+1 ;j<=n;j++)

    5. The dp [j-1] and combined with a [j] is compared to obtain a decision (considering negative)

      temp = max(dp[j-1],temp) + a[j];

    6. Update d [j-1], (optimization, memory)

      dp [j-1] = years;

    7. Update ans, relatively decisions, max (1 decision, decision 2)

      years = max (temp, years);

There are some details, initialization, memory allocation array. The complete code can be seen.

Code

#include<iostream>
#include<string.h>
#define max(a,b) (a>b?a:b)
using namespace std;
int m,ans;
long long n;
int a[1000005];
int dp[1000005];
int main()
{
	while((scanf("%d %d",&m,&n) )!=EOF) //cin>>m>>n;
	{
		for(int i=1;i<=n;i++)
			cin>>a[i];
		memset(dp,0,sizeof (dp) );
		for(int i=1;i<=m;i++)
		{
			int temp=0;//***
			for(int k =1;k<=i;k++)
			{
				temp += a[k];
			}
			ans = temp;
			for(int j = i+1 ;j<=n;j++)
			{
				temp = max(dp[j-1],temp) + a[j];
				dp[j-1] = ans;
				ans = max(ans,temp);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
	
}

Guess you like

Origin blog.csdn.net/DanBo_C/article/details/89976922