DP (Interval Topic 1)

Keywords: iOS

Question: There are several snacks n, packed in pipes (No. 1 n1-n1 n). Now you can take out a box of snacks every day. The contribution of each box of snacks to the answer is that the price of the snack multiplied by the number of days it was taken. Ask what is the maximum value.

>> face <<

Strategy: Memorized Search or Interval dp

State: DP [l] [r] - > Maximum revenue in this area

Target: Maximum revenue in the whole range of dp[1][n]dp[1][n]dp[1][n]

BOUNDARY: The contribution of the first transfer is provided entirely by the price of snacks. No additional boundary (memory search) is required. Enumeration of all sub-intervals with 1 boundary length is a [i]* n, representing the maximum benefit of the interval.

Legitimate Judgment: No

Transfer equation: interval dp:

dp[l][r]=max(dp[l][r−1]+(n−len+1)∗a[r],dp[l+1][r]+(n−len+1)∗a[l]);dp[l][r] = max(dp[l][r - 1] + (n - len + 1) * a[r], dp[l + 1][r] + (n - len + 1) * a[l]);dp[l][r]=max(dp[l][r−1]+(n−len+1)∗a[r],dp[l+1][r]+(n−len+1)∗a[l]);

attention: Boundary

Double experience:

@author: jasonleft -- Memorization
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rev(i, a, b) for (int i = (a); i >= (b); --i)
#define _for(i, a, b) for (int i = (a); i < (b); ++i)
#define _rof(i, a, b) for (int i = (a); i > (b); --i)
#define ll long long
#define db double
#define oo 0x3f3f3f3f
#define eps 0.00001
#define all(x) x.begin(), x.end()
#define met(a, b) memset(a, b, sizeof(a))
#define what_is(x) cerr << #x << " is " << x << endl
#define lowbit(x) x &(-x)
using namespace std;
const int maxn = 2e3 + 5;
const int mod = 9999973;
int a[maxn], n, dp[maxn][maxn], ans;
int dfs(int day, int l, int r)
{
	if(r < l)return 0;
	if(dp[l][r])return dp[l][r];
	return dp[l][r] = max(dfs(day+1,l+1,r)+a[l]*day, dfs(day+1, l, r - 1)+a[r] * day);
}
int main()
{
	ios::sync_with_stdio(0);
	cin >> n;
	_rep(i, 1, n) cin >> a[i];
	
	cout <<dfs(1, 1, n) << endl;
}
@author: jasonleft -- Section dp
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rev(i, a, b) for (int i = (a); i >= (b); --i)
#define _for(i, a, b) for (int i = (a); i < (b); ++i)
#define _rof(i, a, b) for (int i = (a); i > (b); --i)
#define ll long long
#define db double
#define oo 0x3f3f3f3f
#define eps 0.00001
#define all(x) x.begin(), x.end()
#define met(a, b) memset(a, b, sizeof(a))
#define what_is(x) cerr << #x << " is " << x << endl
#define lowbit(x) x &(-x)
using namespace std;
const int maxn = 2e3 + 5;
const int mod = 9999973;
int a[maxn], n, dp[maxn][maxn], ans;

int main()
{
	ios::sync_with_stdio(0);
	cin >> n;
	_rep(i, 1, n) cin >> a[i];
	_rep(i, 1, n) dp[i][i] = a[i] * n; //Why multiply n? dp[i][j] represents the greatest value in its interval, so multiply n
	_rep(len, 2, n)
	{
		_rep(l, 1, n - len + 1)
		{
			int r = l + len - 1;
			dp[l][r] = max(dp[l][r - 1] + (n - len + 1) * a[r], dp[l + 1][r] + (n - len + 1) * a[l]);
		}
	}
	cout << dp[1][n] << endl;
}

Posted by Invincible on Mon, 14 Oct 2019 08:22:53 -0700