Delete number (section DP)

Title Description

There are N different positive integers x1, x2, ... xN in a row, we can remove the continuous i (1≤i≤n) number (the number can only be deleted from both sides) from the left or right, left the number of Ni, then the number of the remaining operations by the above process until all numbers are deleted.

Each operation has a value of operation, for example, now you want to delete all the numbers from i k position to position. Operation value of | xi - xk | * (k-i + 1), if only remove a number, the value of the value of the operation number. Ask how you can get the maximum value, the maximum value of the find operation.

Input Format

Conduct a first positive integer N;

The second line has spaced apart by spaces N different positive integer number N.

Output Format

Line, contains a positive integer, the maximum value for the operation

Sample input and output

Input # 1 copy

6
54 29 196 21 133 118

Output # 1 copy

768

Description / Tips

[Sample Description]

Described, can be obtained through three times the maximum operation, first remove the front 54,29,196 number 3, the operation value of 426. The second operation is to remove the last digit in the number of the remaining 118 (21 133 118) three, the value of 118 operations. A third operation to remove the remaining number of 2 133 and 21, the operation value of 224. The total value of the operation 426 + 118 + 224 = 768.

[Data] scale

3≤N≤100, N operand is an integer between 1..1000.

 

Ideas:

  1. Because the data is a positive integer, so the data should be deleted from the left to start
  2. From recursive train of ideas, a maximum attainable interval, equals | xi - xk | * (k-i + 1) i, k its ends, or the value is a value between the two cells, and composition, that is about the same as the termination condition endpoint interval (a number, for the maximum number itself)
  3. Thinking of the dynamic programming to do recursive formula: dp [i] [j] = max (| xi - xk | * (k-i + 1), dp [i] [k] + dp [k + 1] [ j]), i <= k <= j
#include<iostream>
#include<algorithm>
#include<cmath>
#define j i+k   //宏定义 
using namespace std;
int main() {
	int n,a[102],dp[102][102]= {0};  //dp[i][j]表示区间【i,j】的最大值 
	cin>>n;
	for(int i=1; i<=n; i++) {
		cin>>a[i];
		dp[i][i]=a[i];   //特判 
	}
	for(int k=1; k<n; k++) {  //枚举j-i的值 
		for(int i=1; j<=n; i++) {  //枚举i 
			int maxl=abs(a[i]-a[j])*(k+1);
			for(int l=i; l<j; l++)maxl=max(maxl,dp[i][l]+dp[l+1][j]);
			dp[i][j]=maxl;
		}
	}
	cout<<dp[1][n]<<endl;   
	return 0;
}

Optimization: double loop may be modified, ideas: subject of the request for the entire interval, and not part of the interval, it can be considered recursive dp [1] [j] = max (dp [1] [k] + dp [ k + 1] [j], dp [1] [j], each corresponding to find such dp [i] [j] value is maximum value interval last independently (a value of difference between both ends ..), so that, with this double loop, each interval needs to be treated as an independent value range is initialized

for (int j=i;j<=n;j++)
    for (int k=i;k<j;k++)//枚举删除范围
      dp[1][j]=max(dp[1][k]+dp[k+1][j],dp[1][j]);//取最大价值

 

Published 42 original articles · won praise 16 · views 3407

Guess you like

Origin blog.csdn.net/qq_41542638/article/details/98130313