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. Test typora

    目录 0. test 0.5 easy test 1. problem 1 2. problem 2 3. problem 3 import numpy as np import matplotlib ...

  2. LeetCode404.左叶子之和

    题目 法一.广度优先搜索 1 class Solution { 2 public: 3 int sumOfLeftLeaves(TreeNode* root) { 4 if(root == NULL) ...

  3. ctfhub技能树—RCE—命令注入

    打开靶机 查看页面信息 输入127.0.0.1进行测试 构造payload 127.0.0.1&ls 查看文件内容信息 127.0.0.1 & cat 179852221619745. ...

  4. leetcode刷题录-1395

    目录 题目 思考过程 查看别人分享的思路 总结 题目 题目地址:https://leetcode-cn.com/problems/count-number-of-teams/ n 名士兵站成一排.每个 ...

  5. 痞子衡嵌入式:MCUBootFlasher v3.0发布,为真实的产线操作场景而生

    -- 痞子衡维护的NXP-MCUBootFlasher工具(以前叫RT-Flash)距离上一个版本(v2.0.0)发布过去一年半以上了,这一次痞子衡为大家带来了全新版本v3.0.0,从这个版本开始,N ...

  6. Python赋值、浅复制和深复制

    Python赋值.浅复制和深复制 ​ 首先我们需要知道赋值和浅复制的区别: 赋值和浅复制的区别 赋值,当一个对象赋值给另一个新的变量时,赋的其实是该对象在栈中的地址,该地址指向堆中的数据.即赋值后,两 ...

  7. linux--关于JVM CPU资源占用过高的问题排查

    一.背景: 先执行一个java程序里面开了两个线程分别都在while循环做打印操作. # java -cp ./test-threads.jar com.spiro.Main 二.现象: 通过top命 ...

  8. 支持 gRPC 长链接,深度解读 Nacos 2.0 架构设计及新模型

    支持 gRPC 长链接,深度解读 Nacos 2.0 架构设计及新模型 原创 杨翊(席翁) 阿里巴巴云原生 2020-12-28    

  9. [BJOI2016]水晶 做题心得

    [BJOI2016]水晶 做题心得 这是一个写了我两小时的傻逼题.写这个题浪费了一堆时间后,我才意识到我码力又不行了.于是整理起了实现技巧,开始练码力. 思路 不难.首先把 \((x,y,z)\) 变 ...

  10. noip 注意事项 (个人向)

    目录 非常重要 对拍 空间 极限数据 模数,YES/NO等大小写 个人 考场 神仙 czdzx 说要写,我也来写 非常重要 对拍 空间 极限数据 模数,YES/NO等大小写 个人 养身体,不要紧张,不 ...