其他分享
首页 > 其他分享> > [USACO21FEB] Modern Art 3 G 题解

[USACO21FEB] Modern Art 3 G 题解

作者:互联网

这么简单的区间dp我居然写挂了嘤嘤嘤

题解

主要是分为两种情况:

#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn=302;
int f[maxn][maxn],c[maxn];
int main()
{
//	freopen("paint.in","r",stdin);
//	freopen("paint.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&c[i]);
	memset(f,0x3f,sizeof(f));
	for(int i=1;i<=n;i++)
		f[i][i]=1;
	for(int l=2;l<=n;l++)
	{
		for(int i=1;i+l-1<=n;i++)
		{
			int j=i+l-1;
			if(c[i]==c[j])
			{
				f[i][j]=min(f[i][j],min(f[i+1][j],f[i][j-1]));
			}
			else
			{
				for(int k=i;k<=j;k++)
				{
					f[i][j]=min(f[i][k-1]+f[k][j],f[i][j]);
				}
			}

		}
	}
	printf("%d",f[1][n]);
}

标签:Art,int,题解,Modern,maxn,区间,颜色,考虑,dp
来源: https://www.cnblogs.com/cc0000/p/15552289.html