hidocoder 1636 : Pangu and Stones 区间DP

描述

In Chinese mythology, Pangu is the first living being and the creator of the sky and the earth. He woke up from an egg and split the egg into two parts: the sky and the earth.

At the beginning, there was no mountain on the earth, only stones all over the land.

There were N piles of stones, numbered from 1 to N. Pangu wanted to merge all of them into one pile to build a great mountain. If the sum of stones of some piles was S, Pangu would need S seconds to pile them into one pile, and there would be S stones in the new pile.

Unfortunately, every time Pangu could only merge successive piles into one pile. And the number of piles he merged shouldn’t be less than L or greater than R.

Pangu wanted to finish this as soon as possible.

Can you help him? If there was no solution, you should answer ‘0’.
输入

There are multiple test cases.

The first line of each case contains three integers N,L,R as above mentioned (2<=N<=100,2<=L<=R<=N).

The second line of each case contains N integers a1,a2 …aN (1<= ai <=1000,i= 1…N ), indicating the number of stones of pile 1, pile 2 …pile N.

The number of test cases is less than 110 and there are at most 5 test cases in which N >= 50.
输出

For each test case, you should output the minimum time(in seconds) Pangu had to take . If it was impossible for Pangu to do his job, you should output 0.
样例输入

3 2 2
1 2 3
3 2 3
1 2 3
4 3 3
1 2 3 4

样例输出

9
6
0

题意: 普通的石子归并上把合并区间的限制从两个改成[L,R]个。

解题思路:
看这题意就要往区间DP上去想。
普通的石子归并只能合并相邻的两个所以开二维数组就行,dp[i][j] 表示i到j区间合并成一堆的最小花费。
然而这里的合并堆数是[L,R],用二维来转移的话 需要枚举i,j之间分成n块,每块的长度是多少 L<=n<=R
这样就需要指数级的时间。
但这个分成n块明显是可以用DP来优化的,只需要在原来的数组上多加一维就好了。
dp[i][j][k] 表示 [i,j]区间合并成k堆的最小花费。 这样一看就很好(nan)转移。
按照这个dp数组的定义的话,如果xjb乱转移,每次转移的花费是>O(n^2) 级别的。 这样总复杂度就不能就大于O(n^5) 肯定不能接受。
但是可以轻(艰)松(难)的想到 每次合并操作,其实就是把几堆合并成一堆, 那么 其实我们只需要在k=1时 才进行真正的合并操作就行了,k!=1 时 我们只要不用进行合并 而是从前面已经得到的状态(既前面合并操作的结果)求出分成 k堆的最小花费而不是合并成k堆的最小花费。
每个状态的转移时间复杂度就降到了O(n) 枚举每个状态的时间是O(n^3) 总复杂度为O(n^4)
在可接受的范围内。
状态转移方程如下:

        int &mins=dp[i][j][k];
                    if(k==1) {
                        for(int s=L; s<=R; s++)
                            mins=min(mins,dp[i][j][s]+per[j]-per[i-1]);
                    } else {
                        for(int e=i;e<j;e++)
                            mins=min(mins,dp[i][e][1]+dp[e+1][j][k-1]);
                    }

附官方题解:这里写图片描述

#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<queue>
#include<string.h>
#include<cstring>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin);
#define mem(x,y) memset(x,y,sizeof(x));
int a[205];
int per[205];
int dp[105][105][105];
int main() {
    int N,L,R;
    while(~scanf("%d %d %d",&N,&L,&R)) {
        for(int i=1; i<=N; i++) {
            scanf("%d",&a[i]);
            per[i]=per[i-1]+a[i];
        }
        memset(dp,0x3f,sizeof dp);
        for(int i=1;i<=N;i++) dp[i][i][1]=0;
        for(int l=2; l<=N; l++) {
            for(int i=1; i<=N; i++) {
                int j=i+l-1;
                if(j>N) break;
                for(int k=l; k>=1; k--) {
                    int &mins=dp[i][j][k];
                    if(k==1) {
                        for(int s=L; s<=R; s++)
                            mins=min(mins,dp[i][j][s]+per[j]-per[i-1]);
                    } else {
                        for(int e=i;e<j;e++)
                            mins=min(mins,dp[i][e][1]+dp[e+1][j][k-1]);
                    }
                    //cout<<mins<<" "<<dp[i][j][k]<<endl;
                }
            }
        }
        if(dp[1][N][1]==0x3f3f3f3f) puts("0");
        else cout<<dp[1][N][1]<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值