CCF-CSP solution to a problem 201612-4 compression coding

\ (CSP \) also test \ (DP \) Well ... think I'm greedy for two hours in a messy dorm ...

+ Presbyopia or mentally retarded ah ... this is not a problem zone merge bare stone merger Well ... ah ...

Look at this \ (3s \) time limit, \ (O (the n-^ 3) \) are enough, why so long greedy want it ...

#include <bits/stdc++.h>
typedef long long LL;
const int maxn = 1000;

using namespace std;

LL a[maxn + 10];

LL dp[maxn + 10][maxn + 10];

int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%lld", a + i);

    for (int i = 2; i <= n; i++)
        a[i] += a[i - 1];

    memset(dp, 0x3f, sizeof(dp));

    for (int i = 1; i <= n; i++)
        dp[i][i] = 0;

    for (int len = 2; len <= n; len++)
    {
        for (int l = 1; l + len - 1 <= n; l++)
        {
            int r = l + len - 1;
            for (int mid = l; mid <= r - 1; mid++)
            {
                dp[l][r] = min(dp[l][r], dp[l][mid] + dp[mid + 1][r]);
            }
            dp[l][r] += a[r] - a[l - 1];
        }
    }

    if (n == 1)
        printf("%lld\n", a[1]);
    else
        printf("%lld\n", dp[1][n]);

    return 0;
}

Guess you like

Origin www.cnblogs.com/acboyty/p/11387826.html