LibreOJ - 10147 (区间dp)

Posted wanshe-li

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LibreOJ - 10147 (区间dp)相关的知识,希望对你有一定的参考价值。

#pragma warning (disable : 4996)

#include<iostream>
#include<algorithm>
//#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
using namespace std;
#define INF 0x3f3f3f3f
#define inf 0x7FFFFFFF
#define MOD 1000000007
#define pii pair<ll,ll>
#define eps 1e-8
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug******************bug")
#define re  register
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof a)
#define speed std::ios::sync_with_stdio(false)
#define pb push_back
const double Inf = 10000.0;
const double PI = acos(-1.0);
typedef long long ll;
typedef unsigned long long ull;
inline int rd() {
	int X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == ‘-‘; ch = getchar(); }
	while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X;
}

int n, x;
ll maxdp[440][440];
ll mindp[440][440];
int num[440];
void init() {
	for (int i = 0; i <= 2*n; ++i) {
		for (int j = 0; j <= 2*n; ++j) {
			maxdp[i][j] = 0;
			mindp[i][j] = INF;
			if (i == j)	mindp[i][j] = 0;
		}
	}
}

int sc[440];
int main() {
	n = rd();
	init();
	num[0] = 0;
	for (int i = 1; i <= n; ++i) {
		sc[i] = rd();
		sc[i + n] = sc[i];//因为是围成环,所以用双重即可历遍
	}
	for(int i = 1; i <= 2*n; ++ i)
		num[i] = num[i - 1] + sc[i];
	for (int k = 1; k <= n; ++k) {
		for (int l = 1; l+k-1 <= 2 * n; ++l) {
			int e = l + k - 1;
			if (e == l) continue;
			for (int r = l; r < e; ++r) {
				maxdp[l][e] = max(maxdp[l][e], maxdp[l][r] + maxdp[r+1][e] + (num[e] - num[l-1]));
				mindp[l][e] = min(mindp[l][e], mindp[l][r] + mindp[r+1][e] + (num[e] - num[l-1]));
			}
		}
	}
	ll minn = INF, maxx = 0;
	for (int i = 1; i <= n; ++i) {
		minn = min(minn, mindp[i][i + n - 1]);
		maxx = max(maxx, maxdp[i][i + n - 1]);
	}
	printf("%lld
%lld
", minn, maxx);
}

以上是关于LibreOJ - 10147 (区间dp)的主要内容,如果未能解决你的问题,请参考以下文章

loj10147. 「一本通 5.1 例 1」石子合并

信息学奥赛一本通 5.1 区间类动态规划

LibreOJ 6278 数列分块入门 2(分块区间加法,二分)

loj516 「LibreOJ β Round #2」DP 一般看规律

单调队列优化DP烽火传递 LibreOJ - 10180

LibreOJ 6277. 数列分块入门 1