莫名其妙的ac,不知道是怎么回事,求助 :(
查看原帖
莫名其妙的ac,不知道是怎么回事,求助 :(
1227031
ARTI001楼主2024/1/24 17:10

为什么我的这个最大值每次都是答案的两倍,改成ans2/2之后就ac了,不理解,求助 :(

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

int n;
int a[205];
int dp1[205][205],dp2[205][205];
int s[205];

int main()
{
	cin>>n;
	memset(dp1,0x3f,sizeof(dp1));
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		a[i+n]=a[i];
	}
	for(int i=1;i<=2*n;i++)
	{
		s[i]=s[i-1]+a[i];
		dp1[i][i]=0; 
	}
	
	for(int num=2;num<=n;num++)
	{
		for(int sta=1;sta+num-1<=2*n;sta++)
		{
			int dest=sta+num-1;
			for(int i=sta;i<=dest;i++)
			{
				dp1[sta][dest]=min(dp1[sta][dest],dp1[sta][i]+dp1[i+1][dest]+s[dest]-s[sta-1]);
				dp2[sta][dest]=max(dp2[sta][dest],dp2[sta][i]+dp2[i+1][dest]+s[dest]-s[sta-1]);
			}
		}
	}
	int ans1=dp1[1][n],ans2=0;
	for(int i=1;i<=n;i++)
	{
		ans1=min(dp1[i][i+n-1],ans1);
		ans2=max(dp2[i][i+n-1],ans2);
	}
	cout<<ans1<<endl<<ans2;
	return 0;
}
2024/1/24 17:10
加载中...