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. my购物车

    sum=0 a=input("请输入“水果”或“衣服”:") if a=="手机": while True: shop = { '蓝葡萄', '水蜜桃', '草 ...

  2. Python基础-函数参数

    Python基础-函数参数 写在前面 如非特别说明,下文均基于Python3 摘要 本文详细介绍了函数的各种形参类型,包括位置参数,默认参数值,关键字参数,任意参数列表,强制关键字参数:也介绍了调用函 ...

  3. drf 视图功能

    视图 drf提供的视图功能 自己的第一次封装 #一个功能写成一个类,方便组合,只要继承它就可以有这个功能 #将功能都写在一个类中,可控性就会变差 from book.myserializers imp ...

  4. List删除元素

    在单线程环境下的解决办法 public void remove() { if (lastRet == -1) throw new IllegalStateException(); checkForCo ...

  5. 水题:UVa213- Message Decoding

    Message Decoding Some message encoding schemes require that an encoded message be sent in two parts. ...

  6. Python 前端的第三方库

    sweetalert sweeralert:地址 这个使用很简单,需要在在他们的,css和js文件. 酷炫的结果    datatables datatables:地址 已https://datata ...

  7. 29、在android中采用动画方案来弹出窗口

    1.自定义style 2.使用方法: buttonWhat.setOnClickListener(new OnClickListener() { @Override public void onCli ...

  8. day05_04 数据类型-数值、布尔值、字符串简介

    数据运算 数据类型初识 数字 整数 int(integer) 整型 长整型 在python3里面已经不区分整型和长整型了,统一都叫整型 在32位机器上,整数的位数为32位,取值范围为-2**31-2* ...

  9. Python字典类型、

    字典类型: # msg_dic = {#     'apple': 10,#     'tesla': 100000,#     'mac': 3000,#     'lenovo': 30000,# ...

  10. uiautomator 一个简单脚本创建流程

    http://www.codeceo.com/article/android-ui-auto-test.html