「THUSC 2016」成绩单

BZOJ-4897 LOJ-2292

#include 
#include 
#define Min(_A, _B) (_A < _B ? _A : _B)
#define Max(_A, _B) (_A > _B ? _A : _B)
#define cmin(_A, _B) (_A > _B ? _A = _B : 0);
#define R register
int n, a, b, w[55], c[55], f[55][55], d[1010], g[55][55][55][55], dd[1010], tot;
int main()
{
    scanf("%d %d %d", &n, &a, &b);
    memset(f, 63, sizeof(f));
    memset(g, 63, sizeof(g));
    R int Inf = f[0][0];
    for(R int i = 1; i <= n; i++) scanf("%d", &w[i]), d[w[i]] = 1;
    for(R int i = 1; i <= 1001; i++) d[i] += d[i - 1]; tot = d[1001];
    for(R int i = 1; i <= n; i++) c[i] = d[w[i]], dd[c[i]] = w[i]; 
    for(R int i = 1; i <= n; i++) f[i][i] = a, g[i][i][c[i]][c[i]] = 0, f[i][i - 1] = 0;
    for(R int l = 2; l <= n; l++)
        for(R int i = 1, j; (j = i + l - 1) <= n; i++)
        {
            cmin(g[i][j][c[j]][c[j]], f[i][j - 1]);
            for(R int u = 1; u <= tot; u++)
                for(R int v = 1; v <= u; v++)
                    for(R int k = i; k < j; k++)
                        cmin(g[i][j][Max(u, c[j])][Min(v, c[j])], g[i][k][u][v] + f[k + 1][j - 1]);
            for(R int u = 1; u <= tot; u++)
                for(R int v = 1; v <= u; v++)
                {   
                    cmin(f[i][j], g[i][j][u][v] + a + b * (dd[u] - dd[v]) * (dd[u] - dd[v]));
                    for(R int k = i; k < j; k++)
                        cmin(f[i][j], g[i][k][u][v] + f[k + 1][j] + a + b * (dd[u] - dd[v]) * (dd[u] - dd[v]));
                }   
        }
    printf("%d\n", f[1][n]);
    return 0;
}

你可能感兴趣的:(动态规划)