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 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;
}

hdu3506 Monkey Party的更多相关文章

  1. hdu3506 Monkey Party (区间dp+四边形不等式优化)

    题意:给n堆石子,每次合并相邻两堆,花费是这两堆的石子个数之和(1和n相邻),求全部合并,最小总花费 若不要求相邻,可以贪心地合并最小的两堆.然而要求相邻就有反例 为了方便,我们可以把n个数再复制一遍 ...

  2. HDU-3506 Monkey Party (环形石子合并)

    题目大意:n堆石子围成一圈,每堆石子的块数已知,每次可以将相邻的两堆合并到一堆,块数变为两堆之和,代价也为两堆石子块数之和.求合并到一堆的最小代价. 题目分析:先通过将前n-1依次个移到第n个后面,将 ...

  3. HDU3506 Monkey Party (区间DP)

    一道好题...... 首先要将环形转化为线形结构,接着就是标准的区间DP,但这样的话复杂度为O(n3),n<=1000,要超时,所以要考虑优化. dp[i][j]=min( dp[i][k]+d ...

  4. 【初学python】使用python调用monkey测试

    目前公司主要开发安卓平台的APP,平时测试经常需要使用monkey测试,所以尝试了下用python调用monkey,代码如下: import os apk = {'j': 'com.***.test1 ...

  5. Monkey Patch/Monkey Testing/Duck Typing/Duck Test

    Monkey Patch Monkey Testing Duck Typing Duck Test

  6. monkey命令选项参考

    基本参数:     --help              打印帮助消息 -v  可以在命令行中出现多次,每次一个-V选项都会增加monkey向命令行打印输出的详细级别.默认的级别0只会打印启动信息. ...

  7. monkey之monkey日志分析

    一.初步分析方法:Monkey测试出现错误后,一般的差错步骤为以下几步:1.找到是monkey里面的哪个地方出错2.查看Monkey里面出错前的一些事件动作,并手动执行该动作3.若以上步骤还不能找出, ...

  8. monkey之monkey命令详解

    四大类-- 常用选项.事件选项.约束选项.调试选项 1.常用选项 --help:打印帮助信息 -v:指定打印信息的详细级别,一个-v增加一个级别 ,默认级别为 0 .用于指定反馈信息级别(信息级别就是 ...

  9. monkey之三:monkey测试测略(摘抄)

    一.分类 Monkey测试针对不同的对象,不同的目的,采用不同的测略方案. 测试类型分为: 应用程序的稳定性测试和压力测试 测试对象分为: 单个APK和多个APK集合 测试目的分为: 解决问题的测试( ...

随机推荐

  1. python学习笔记 | 列表去重

    ''' @author: 人人都爱小雀斑 @time: 2020/3/10 10:29 @desc: ''' L=[1,5,7,4,6,3,0,5,8,4,4] 方法1:for循环 L1=[] for ...

  2. 【ORA】ORA-00371: not enough shared pool memory

    今天rac中有一个节点asm实例起不来包了ora-000371的错误,错误贴在下面: [oracle@rac2 dbs]$ srvctl start asm -n rac2 PRKS-1009 : F ...

  3. Linux Shell 编程基础详解——吐血整理,墙裂推荐!

    第一部分:Linux Shell 简介 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序, ...

  4. vxfs(Veritas File System)扩充目录大小

    1.新增加一个磁盘并初始化 # vxdisk list # vxdisksetup -i 3pardata0_22 2.将新增加的磁盘合并到磁盘组中 # vxdg -g testdg01 adddis ...

  5. SAP RFC的相关的术语说明

    工作比较忙,很少有时间写点文章,抽空写点吧,给需要的人看看,虽然徒弟很多了,不过还是不要固步自封,在这里也指导更多的人进步吧. RFC(Remote Function Call)是SAP系统和其他(S ...

  6. 2V转5V输出,2.4V转5V输出,DC-DC同步整流升压电路

    PW5100可以适用于2V转5V和2.4V转5V的应用电路中,PW5100是一颗DC-DC的同步升压转换器芯片. PW5100特点: 1, 低输入,宽范围:0.7V-5V 2, 输出电压固定,外围少: ...

  7. EntityFramework Core如何映射动态模型?

    前言 本文我们来探讨下映射动态模型的几种方式,相信一部分童鞋项目有这样的需求,比如每天/每小时等生成一张表,此种动态模型映射非常常见,经我摸索,这里给出每一步详细思路,希望能帮助到没有任何头绪的童鞋, ...

  8. Centos7 添加用户及设置权限

    一.添加用户 1.登录root 用户 [gau@localhost /]$ su Password: # 输入密码 [root@localhost /]# 2.添加用户 [root@localhost ...

  9. Vue中:error 'XXXXX' is not defined no-undef解决办法

    Vue中:error 'XXXXX' is not defined no-undef解决办法 报错内容: × Client Compiled with some errors in 7.42s √ S ...

  10. Boyer-Moore 投票算法

    Boyer-Moore 投票算法 http://theory.stanford.edu/~trevisan/cs154-12/notestream.pdf 众数