loj10147. 「一本通 5.1 例 1」石子合并

思路:
  经典的区间dp,题解到处都是。。。

#include
#include
#include
using namespace std;
const int maxn = 1010;
inline void qread(int &x){
    x = 0;
    register int ch = getchar();
    while(ch < '0' || ch > '9')    ch = getchar();
    while(ch >= '0' && ch <= '9')    x = 10 * x + ch - 48, ch = getchar();
}
int f[maxn << 1][maxn << 1];
int g[maxn << 1][maxn << 1];
int sum[maxn];
int n;
int data[maxn << 1];
int main(void){
    qread(n);
    for(int i=1; i<=n; ++i){
        qread(data[i]);
        data[i+n] = data[i];
    }
    for(int i=1; i<=2*n-1; ++i)
        sum[i] = sum[i-1] + data[i];
    for(int L = 2; L <= n; ++L)
        for(int i = 1; i <= 2 * n - L + 1; ++i){
            int j = i + L - 1;
            f[i][j] = 0x3f3f3f3f;
            for(int k = i; k < j; ++k){
                f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j]);
                g[i][j] = max(g[i][j], g[i][k] + g[k + 1][j]);
            }
            f[i][j] += sum[j] - sum[i-1];
            g[i][j] += sum[j] - sum[i-1];
        }
    int fans = 0x3f3f3f3f, gans = 0;
    for(int i=1; i<=n; ++i)    fans = min(fans, f[i][i + n -1]);
    for(int i=1; i<=n; ++i)    gans = max(gans, g[i][i + n -1]);
    printf("%d\n%d\n", fans, gans);        
}

 

转载于:https://www.cnblogs.com/junk-yao-blog/p/9486375.html

你可能感兴趣的:(loj10147. 「一本通 5.1 例 1」石子合并)