Pangu and Stones HihoCoder - 1636
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'.
Input
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.
Output
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.
Sample Input
3 2 2
1 2 3
3 2 3
1 2 3
4 3 3
1 2 3 4
Sample Output
9
6
0
题意:
给出n堆石头,每次最少合并其中l堆,最多合并r堆,问合成1堆最少需要花费多少时间
分析:
dp[i][j][k]
d
p
[
i
]
[
j
]
[
k
]
表示
i−j
i
−
j
这个区间合成k堆所需要的最小时间,故可得状态转移方程式:
d为枚举的区间间隔
1.
k==1
k
==
1
时:
dp[i][i+d][1]=min(dp[i][i+d][1],dp[i][j][k]+dp[j+1][i+d][1]+sum[i][i+d])
d
p
[
i
]
[
i
+
d
]
[
1
]
=
m
i
n
(
d
p
[
i
]
[
i
+
d
]
[
1
]
,
d
p
[
i
]
[
j
]
[
k
]
+
d
p
[
j
+
1
]
[
i
+
d
]
[
1
]
+
s
u
m
[
i
]
[
i
+
d
]
)
(l−1<=k<=r−1)
(
l
−
1
<=
k
<=
r
−
1
)
分析一下这里我们想把区间
[i,i+d]
[
i
,
i
+
d
]
的堆合成一堆,这样我们将区间分成两部分
[i,j]
[
i
,
j
]
和
[j+1,i+d]
[
j
+
1
,
i
+
d
]
其中令区间
[i,j]
[
i
,
j
]
目前假设k堆石头,那么我们令区间
[j+1,i+d]
[
j
+
1
,
i
+
d
]
有一堆石头,那么合成一堆相当于把一共k+1堆石头合成一堆,所以花费时间为之前左区间合成k堆石头花费时间+右区间合成一堆石头花费时间,最后加上合成后的总的时间即
sum[i][i+d]
s
u
m
[
i
]
[
i
+
d
]
,此时便需要用到限制条件因为我们把
k+1
k
+
1
堆合成一堆所以用该满足
L≤k+1≤R
L
≤
k
+
1
≤
R
即
L−1≤k≤R−1
L
−
1
≤
k
≤
R
−
1
2. k≥2 k ≥ 2 时:
dp[i][i+d][k]=min(dp[i][i+d][k],dp[i][j][k−1]+dp[j+1][i+d][1]) d p [ i ] [ i + d ] [ k ] = m i n ( d p [ i ] [ i + d ] [ k ] , d p [ i ] [ j ] [ k − 1 ] + d p [ j + 1 ] [ i + d ] [ 1 ] )
我们想要将区间 [i,i+d] [ i , i + d ] 的石头堆合成k堆可以分成左区间合成 k−1 k − 1 堆,右区间合成1堆
此处k不用做限制:
因为我们观察右区间是合成一堆,是我们上面第一种情况已经求完的一定满足限制,那么左区间的
k−1
k
−
1
堆石头,最小
k−1
k
−
1
是1也就是一共合成两堆,这样相当于两边区间都是合成一堆,而合成一堆上面第一种情况已经满足了限制,这里就一定满足限制,同理,k增大,所有情况都是由1堆的情况递推过来的,所以不需要限制,直接相加结果
code:
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
const int inf = 0x3f3f3f3f;
int n,l,r;
int w[N];
int sum[N][N];
int dp[N][N][N];
int main(){
while(~scanf("%d%d%d",&n,&l,&r)){
for(int i = 1; i <= n; i++){
scanf("%d",&w[i]);
}
memset(dp,inf,sizeof(dp));
for(int i = 1; i <= n; i++){
sum[i][i-1] = 0;
for(int j = i; j <= n; j++){
sum[i][j] = sum[i][j-1] + w[j];
dp[i][j][j-i+1] = 0;
}
}
for(int d = 1; d <= n; d++){
for(int i = 1; i + d <= n; i++){
for(int j = i; j <= i + d - 1; j++){
for(int k = l-1; k <= r - 1; k++){
dp[i][i+d][1] = min(dp[i][i+d][1],dp[i][j][k]+dp[j+1][i+d][1]+sum[i][i+d]);
}
}
for(int k = 2; k <= d; k++){
for(int j = i; j <= i + d - 1; j++){
dp[i][i+d][k] = min(dp[i][i+d][k],dp[i][j][k-1]+dp[j+1][i+d][1]);
}
}
}
}
if(dp[1][n][1] == inf)
puts("0");
else
printf("%d\n",dp[1][n][1]);
}
return 0;
}