首页 技术 正文
技术 2022年11月11日
0 收藏 338 点赞 5,014 浏览 2242 个字

Problem DescriptionFar 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. 
 

InputThere 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. 

OutputFor each case, you should print a line giving the mininal time SDH needs on introducing. 

Sample Input


8
5 2 4 7 6 1 3 9 

Sample Output


105

这题是环状的石子合并问题,把长度为n的环变为长度为2*n-1的链就行。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 1005
#define inf 999999999
int a[2*maxn],sum[2*maxn],s[2*maxn][2*maxn];
ll dp[2*maxn][2*maxn];
int main()
{
int n,m,i,j,len,t,k;
ll minx;
while(scanf("%d",&n)!=EOF)
{
sum[0]=0;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
a[i+n]=a[i];
sum[i]=sum[i-1]+a[i];
dp[i][i]=0;
}
for(i=n+1;i<=2*n-1;i++){
sum[i]=sum[i-1]+a[i];
dp[i][i]=0;
} for(i=1;i<2*n-1;i++){
dp[i][i+1]=a[i]+a[i+1];
s[i][i+1]=i;
}
if(n==1){
printf("0\n");continue;
}
else if(n==2){
printf("%d\n",a[1]+a[2]);
continue;
} minx=inf;
for(len=3;len<=n;len++){
for(i=1;i+len-1<=2*n-1;i++){
j=i+len-1;
dp[i][j]=inf;
for(k=s[i][j-1];k<=s[i+1][j];k++){
if(dp[i][j]>dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]){
dp[i][j]=dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1];
s[i][j]=k;
}
}
if(len==n){
if(i==1)minx=dp[1][n];
else minx=min(minx,dp[i][i+n-1]);
}
}
}
/*minx=dp[1][n];
for(i=2;i<=n;i++){
minx=min(minx,dp[i][i+n-1]);
}*/ printf("%lld\n",minx);
}
return 0;
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295