Discription
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

发现这是一个四边形不等式的模板题
#include<bits/stdc++.h>
#define ll long long
#define maxn 2005
using namespace std;
int f[maxn][maxn],n,m,w[maxn];
int pos[maxn][maxn],ans,l,r;
int main(){
while(scanf("%d",&n)==1){
ans=1<<30;
memset(f,0x3f,sizeof(f));
for(int i=1;i<=n;i++){
scanf("%d",w+i),w[i+n]=w[i];
f[i][i]=f[i+n][i+n]=0;
pos[i][i]=i,pos[i+n][i+n]=i+n;
}
m=n<<1;
for(int i=1;i<=m;i++) w[i]+=w[i-1]; for(int len=1;len<n;len++)
for(int i=1,j;(j=i+len)<=m;i++){
l=pos[i][j-1],r=pos[i+1][j];
for(int u=l;u<=r;u++) if(f[i][u]+f[u+1][j]<f[i][j]){
f[i][j]=f[i][u]+f[u+1][j];
pos[i][j]=u;
}
f[i][j]+=w[j]-w[i-1];
} for(int i=1;i<=n;i++) ans=min(ans,f[i][i+n-1]);
// for(int i=1;i<m;i++)
// for(int j=i;j<=m;j++) printf("%d %d:%d, %d \n",i,j,f[i][j],pos[i][j]); printf("%d\n",ans);
} return 0;
}

  

 

Hdoj 3506 Monkey Party的更多相关文章

  1. 【HDU】3506 Monkey Party

    http://acm.hdu.edu.cn/showproblem.php?pid=3506 题意:环形石子合并取最小值= =(n<=1000) #include <cstdio> ...

  2. hdu 3506 Monkey Party 区间dp + 四边形不等式优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3506 四边行不等式:http://baike.baidu.com/link?url=lHOFq_58V-Qpz_ ...

  3. HDU - 3506 Monkey Party

    HDU - 3506 思路: 平行四边形不等式优化dp 这不就是石子归并(雾 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma G ...

  4. HDU 3506 Monkey Party(区间DP)题解

    题意:有n个石堆排成环,每次能合并相邻的两堆石头变成新石堆,代价为新石堆石子数,问最少的总代价是多少 思路:先看没排成环之前怎么做:用dp[i][j]表示合并i到j所需的最小代价,那么dp[i][j] ...

  5. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  6. 区间DP入门题目合集

      区间DP主要思想是先在小区间取得最优解,然后小区间合并时更新大区间的最优解.       基本代码: //mst(dp,0) 初始化DP数组 ;i<=n;i++) { dp[i][i]=初始 ...

  7. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

  8. 【HDOJ】1069 Monkey and Banana

    DP问题,我是按照边排序的,排序既要考虑x也要考虑y,同时在每个面中,长宽也要有序.还有注意状态转移,当前高度并不是之前的最大block叠加的高度,而是可叠加最大高度+当前block高度或者是当前bl ...

  9. 【HDOJ】1512 Monkey King

    左偏树+并查集.左偏树就是可合并二叉堆. /* 1512 */ #include <iostream> #include <string> #include <map&g ...

随机推荐

  1. perl-tips-1

    .pm 应该保存 Perl Module,也就是 Perl 模块.例如 Socket.pm.pl 应该保存 Perl Library,也就是 Perl 库文件.例如 perldb.pl.plx 应该保 ...

  2. CDH4 journalnode方式手工安装手册之一

    一.                                环境部署概况   cdh-master 172.168.10.251 cdh-node1 172.168.10.251 cdh-no ...

  3. 【转】git bash here 右键菜单失效后的修复方法

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\git_shell] @="Git Ba&sh Here"   [ ...

  4. luogu2158 [SDOI2008]仪仗队 欧拉函数

    点 $ (i,j) $ 会看不见当有 $ k|i $ 且 $ k|j$ 时. 然后就成了求欧拉函数了. #include <iostream> #include <cstring&g ...

  5. 大数据学习——actor编程

    1 概念 Scala中的Actor能够实现并行编程的强大功能,它是基于事件模型的并发机制,Scala是运用消息(message)的发送.接收来实现多线程的.使用Scala能够更容易地实现多线程应用的开 ...

  6. WordPress后台添加侧边栏菜单

    add_action('admin_menu', 'register_custom_menu_page'); function register_custom_menu_page() { add_me ...

  7. 2018 “百度之星”程序设计大赛 - 初赛(B)

    degree  Accepts: 1581  Submissions: 3494  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 1310 ...

  8. javascript学习笔记 - 引用类型 基本包装类型

    六 基本包装类型 Boolean,Number,String 这三个引用类型亦称为基本包装类型,与基本的数据类型boolean,number,string相关联.为了方便操作这些基本类型的数据. 引用 ...

  9. Eclipse调试程序及项目的导入导出

    Eclipse调试程序 调试概述: ①   调试就是测试程序的方法,主要的目的就是解决程序的逻辑问题,流程是:发现问题.修改问题.正确执行; ②   以前我们可以使用System.out.printl ...

  10. 测试openssl_encrypt

    <?php //$string = 'It works ? Or not it works ?'; //$pass = '1234'; //$method = 'aes128'; // // / ...