1. 程式人生 > >動態規劃-石子合併圓形

動態規劃-石子合併圓形

#include<cstdio>
#include<algorithm>
using namespace std;
const int INF=1<<30;
int maxx[300][300];
int minn[300][300];
int sum[300];
int box[105];
int main()
{
    int n;
    scanf("%d", &n);
    int t;
    for(int i=1; i<=n; i++)
    {
        scanf("%d", &box[i]);
        sum[i]
=sum[i-1]+box[i]; } for(int i=1; i<=n; i++) { sum[i+n]=sum[i+n-1]+box[i]; } for(int v=2; v<=n; v++) { for(int i=1; i<=2*n-v+1; i++) { int j=i+v-1; maxx[i][j]=-1; minn[i][j]=INF; int temp=sum[j]-sum[i-1
]; for(int k=i; k<j; k++) { maxx[i][j]=max(maxx[i][j], maxx[i][k]+maxx[k+1][j]+temp); minn[i][j]=min(minn[i][j], minn[i][k]+minn[k+1][j]+temp); } } } int Max=-1; int Min=INF; for(int i=1; i<=n; i++) {
if(Max<maxx[i][i+n-1]) Max=maxx[i][i+n-1]; if(Min>minn[i][i+n-1]) Min=minn[i][i+n-1]; } printf("%d\n%d", Min, Max); return 0; }