GCJ

#include <bits/stdc++.h>

using namespace std;
#define maxn 105
#define INF 0x7ffffff

int dp[maxn][maxn];
int Q[maxn];
int p, q;

int main()
{
    while(~scanf("%d%d", &p, &q))
    {
        for(int i=1; i<=q; i++)
            cin>>Q[i];
        Q[0] = 0;
        Q[q+1] = p+1;
        for(int i=0; i<=q; i++)
            dp[i][i+1] = 0;

        for(int l=2; l<=q+1; l++)//根据j-i递增的顺序来递推
            for(int i=0; i+l<=q+1; i++)
        {
            int j = i + l;
            dp[i][j] = INF;
            for(int k=i+1; k<j; k++)
                dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]+Q[j]-Q[i]-2);
        }
        cout<<dp[0][q+1]<<endl;
    }
    return 0;
}

你可能感兴趣的:(ACM,gcj)