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. Logrotate工具使用

    Logrotate ​ logrotate是一个被设计来简化系统管理日志文件的工具,在系统运行时,如果产生大量的日志文件,可以使用该工具进行管理,如/var/log/*文件夹是存储系统和应用日志的目录 ...

  2. innodb日志文件大小

    innodb是用多个文件作为一组循环日志,通常不需要修改默认的日志数量,只修改每个日志文件的大小即可,要修改日志文件大小,需要完全关闭mysql,将旧的日志文件移到其他地方保存,重新配置参数,然后重启 ...

  3. iconv函数报错 Detected an illegal character in input string

    近日使用php代码导出文件为excel,一直乱码.导出修改编码都无效,最后发现,是需要修改php导出代码本身的编码.首先用记事本打开php代码,另存为,选择ANSI格式.然后打开iconv函数这个ph ...

  4. 【MySQL】1托2 ab复制 一个主机两个slave操作手册

    所有实验环境全部是新建的,如果不是新建的mysql一定要备份!!! 环境:CentOS release 6.8 x64 master:192.168.25.100 slave1: 192.168.25 ...

  5. leetcode 1593. 拆分字符串使唯一子字符串的数目最大(DFS,剪枝)

    题目链接 leetcode 1593. 拆分字符串使唯一子字符串的数目最大 题意: 给你一个字符串 s ,请你拆分该字符串,并返回拆分后唯一子字符串的最大数目. 字符串 s 拆分后可以得到若干 非空子 ...

  6. 24V转5V芯片,高输入电压LDO线性稳压器

    PW6206系列是一个高精度,高输入电压低静态电流,高速,低功耗降线性稳压器具有高纹波抑制.输入电压高达40V,负载电流为在VOUT=5V和VIN=7V时高达300mA.该设备采用BCD工艺制造.PW ...

  7. linux自定义位置安装tomcat8.5

    1 下载tomcat安装文件 下载地址:https://tomcat.apache.org/download-80.cgi  2 解压文件 tar -zxvf apache-tomcat-8.5.56 ...

  8. Django orm中related_name/related_query_name区别

    related_name/related_query_name区别 class Department(models.Model): title = models.CharField(verbose_n ...

  9. 拒演"拼命工作"的苦情戏,如何更聪明地提高工作效率?

    前几天PDD的事情又把互联网打工人的工作状态推向了大众视野,引起了大家的口诛笔伐.但是目前来看这种愤慨终究是暂时的,作用甚微.在大环境短时间无法改变的前提下,想想如何应对,或许比在网上愤愤不平破口大骂 ...

  10. 如果using语句中出现异常,资源会被释放掉吗?

    <CLR Via C#>第三版 P489 在using内部抛出了异常,被using的对象还是会被释放掉. Using编译时会自动生成Try Finally代码块. 同样Using只能用于实 ...