码迷,mamicode.com
首页 > 其他好文 > 详细

LibreOJ - 10147 (区间dp)

时间:2020-07-29 09:54:50      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:efi   size   eps   space   warning   time   inf   pac   sig   

#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\n%lld\n", minn, maxx);
}

LibreOJ - 10147 (区间dp)

标签:efi   size   eps   space   warning   time   inf   pac   sig   

原文地址:https://www.cnblogs.com/wanshe-li/p/13394604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!