简单的区间dp Div2 Mar/25/2020 E - Array Shrinking

题目链接
题解连接

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int num[510] = {0};
int f[510][510] = {0};
int w[510][510] = {0};

int read()
{
	int x = 0, w = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if(ch == '-') w = -1;
		ch = getchar();
	}
	while(ch <= '9' && ch >= '0')
	{
		x = 10 * x + ch - '0';
		ch = getchar();
	}
	return x * w;
}

int main()
{
	int n = read();
	memset(f, 50, sizeof(f));
	for(int i = 1; i <= n; i++)
	{
		num[i] = read();
//		if(num[i] == num[i - 1])
//		{
//			f[i - 1][i] = 1;
//			w[i - 1][i] = num[i] + 1;
//		}
		f[i][i] = 1;
		w[i][i] = num[i];
	}
	
	
	for(int i = n - 1; i >= 1; i--)
	{
		for(int j = i + 1; j <= n; j++)
		{
			for(int k = i; k < j; k++)
			{
				if(f[i][k] == 1 && f[k + 1][j] == 1)
				{
					if(w[i][k] == w[k + 1][j] && w[i][k] != 0)
					{
						f[i][j] = 1;
						w[i][j] = w[i][k] + 1;
						break;
					}
				}
				f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j]);
			}
		}
	}
	
	cout << f[1][n];
	
	return 0;
} 
发布了7 篇原创文章 · 获赞 0 · 访问量 146
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览