石子合并:有N堆石子,现要将石子有序的合并成一堆,规定如下:每次只能移动相邻的2堆石子合并,合并花费为新合成的一堆石子的数量。求将这N堆石子合并成一堆的总花费最小(或最大)。
为什么用区间dp我就不解释了,不懂的话网上有很多资料。
现在我来提醒一下写这个dp要注意的问题
1.初态的设置
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
dp[i][j]=INF;
}
}
for(i=1;i<=m;i++)
{
dp[i][i]=0;
}
2.动态方程转移过程
for(len=2;len<=m;len++)//len表示石子堆长度,石子堆最少为两堆,最多为m堆
{
for(i=1;i+len-1<=m;i++)//i表示起点,i从1到m-len+1
{
j=i+len-1;
for(k=i;k<j;k++)//k为什么不能等于j,却能等于i,因为k=i时 表示第一堆和之后的所有的组成的堆一种情况,而k=j无意义
{
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]);
}
}
}
注意每一个变量的含义和取值范围的理由
接下来给一个简单版的石子合并
#include<iostream>
#define N 1000
#define INF 100000000
using namespace std;
int a[N];
int dp[N][N];
int sum[N];
int main()
{
int n;
cin>>n;
while(n--)
{
int m,i,j,k,len;
cin>>m;
int total=0;
sum[0]=0;
for(i=1;i<=m;i++)
{
cin>>a[i];
total+=a[i];
sum[i]=total;
}
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
dp[i][j]=INF;
}
}
for(i=1;i<=m;i++)
{
dp[i][i]=0;
}
for(len=2;len<=m;len++)//len表示石子堆长度,石子堆最少为两堆,最多为m堆
{
for(i=1;i+len-1<=m;i++)//i表示起点,i从1到m-len+1
{
j=i+len-1;
for(k=i;k<j;k++)//k为什么不能等于j,却能等于i,因为k=i时 表示第一堆和之后的所有的组成的堆一种情况,而k=j无意义
{
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]);
}
}
}
cout<<dp[1][m]<<endl;
}
}
进阶版:环形先变成线性的+四边形优化
Monkey PartyTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 4515 Accepted Submission(s): 1662
Problem Description Far away from our world, there is a banana forest. And many lovely monkeys live there. One day, SDH(Song Da Hou), who is the king of banana forest, decides to hold a big party to celebrate Crazy Bananas Day. But the little monkeys don’t know each other, so as the king, SDH must do something.
Now there are n monkeys sitting in a circle, and each monkey has a making friends time. Also, each monkey has two neighbor. SDH wants to introduce them to each other, and the rules are:
1.every time, he can only introduce one monkey and one of this monkey’s neighbor.
2.if he introduce A and B, then every monkey A already knows will know every monkey B already knows, and the total time for this introducing is the sum of the making friends time of all the monkeys A and B already knows;
3.each little monkey knows himself;
In order to begin the party and eat bananas as soon as possible, SDH want to know the mininal time he needs on introducing.
Input There is several test cases. In each case, the first line is n(1 ≤ n ≤ 1000), which is the number of monkeys. The next line contains n positive integers(less than 1000), means the making friends time(in order, the first one and the last one are neighbors). The input is end of file.
Output For each case, you should print a line giving the mininal time SDH needs on introducing.
Sample Input8
5 2 4 7 6 1 3 9
Sample Output105
#include<iostream>
#include<cstdio>
#define N 2000
#define INF 100000000
using namespace std;
int a[N];
int dp[N][N];
int sum[N];
int s[N][N];
int main()
{
int m,i,j,k,len;
while(~scanf("%d",&m))//
{
int total=0;
sum[0]=0;
for(i=1;i<=m;i++)
{
scanf("%d",&a[i]);
total+=a[i];
sum[i]=total;
}
for(i=m+1;i<=2*m;i++)
{
a[i]=a[i-m];
total+=a[i];
sum[i]=total;
}
for(i=1;i<=2*m;i++)
{
dp[i][i]=0;
s[i][i]=i;
}
for(len=2;len<=2*m;len++)//len表示石子堆长度,石子堆最少为两堆,最多为2*m堆
{
for(i=1;i+len-1<=2*m;i++)//i表示起点,i从1到2*m-len+1
{
j=i+len-1;
dp[i][j]=INF;
for(k=s[i][j-1];k<=s[i+1][j];k++)//k为什么不能等于j,却能等于i,因为k=i时 表示第一堆和之后的所有的组成的堆一种情况,而k=j无意义
{
//dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]);
if(dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]<dp[i][j])//我也不太明白为什么
{
dp[i][j]=dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1];
s[i][j]=k;
}
}
}
}
int mm=INF;
for(i=1;i<=m;i++)
{
if(dp[i][i+m-1]<mm)
mm=dp[i][i+m-1];
}
printf("%d\n",mm);
}
}
在写这个的过程中遇到了一个很基础的问题
就是scanf前面为什么要加~,关于这个问题,可以看以下一篇博客scanf是什么意思,为什么scanf前加