Hdoj 3506 Monkey Party
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的更多相关文章
- 【HDU】3506 Monkey Party
http://acm.hdu.edu.cn/showproblem.php?pid=3506 题意:环形石子合并取最小值= =(n<=1000) #include <cstdio> ...
- hdu 3506 Monkey Party 区间dp + 四边形不等式优化
http://acm.hdu.edu.cn/showproblem.php?pid=3506 四边行不等式:http://baike.baidu.com/link?url=lHOFq_58V-Qpz_ ...
- HDU - 3506 Monkey Party
HDU - 3506 思路: 平行四边形不等式优化dp 这不就是石子归并(雾 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma G ...
- HDU 3506 Monkey Party(区间DP)题解
题意:有n个石堆排成环,每次能合并相邻的两堆石头变成新石堆,代价为新石堆石子数,问最少的总代价是多少 思路:先看没排成环之前怎么做:用dp[i][j]表示合并i到j所需的最小代价,那么dp[i][j] ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 区间DP入门题目合集
区间DP主要思想是先在小区间取得最优解,然后小区间合并时更新大区间的最优解. 基本代码: //mst(dp,0) 初始化DP数组 ;i<=n;i++) { dp[i][i]=初始 ...
- 【HDOJ】【1512】Monkey King
数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...
- 【HDOJ】1069 Monkey and Banana
DP问题,我是按照边排序的,排序既要考虑x也要考虑y,同时在每个面中,长宽也要有序.还有注意状态转移,当前高度并不是之前的最大block叠加的高度,而是可叠加最大高度+当前block高度或者是当前bl ...
- 【HDOJ】1512 Monkey King
左偏树+并查集.左偏树就是可合并二叉堆. /* 1512 */ #include <iostream> #include <string> #include <map&g ...
随机推荐
- Python简单试题2
1,给定一组数据,将奇数进行从小到大排序,偶数在原位置不变 例: 原数据:[5, 3, 2, 8, 1, 4] 排序后:[5, 3, 2, 8, 1, 4] 若数组为空,则返回空数组. 代码如下: ...
- java做http接口
问题描述 我要对外提供一个http接口给别人调用...但是我不知道用java怎么做这个接口.请大家详细给我讲讲.从开发到如何发布到服务器.谢谢了 解决方案 如果你这个很简单的话,而且数量也很少,建议直 ...
- TCP的三次握手和四次握手
三次握手(建立连接) 首先,服务器进程(B)先创建传控制块TCB(用来存储连接信息,如连接表,发送和接收序号等),准备接收客户进程(A)的请求.然后服务器进程处于LISTEN(收听)状态,等待客户的连 ...
- POJ 3469 最小割 Dual Core CPU
题意: 一个双核CPU上运行N个模块,每个模块在两个核上运行的费用分别为Ai和Bi. 同时,有M对模块需要进行数据交换,如果这两个模块不在同一个核上运行需要额外花费. 求运行N个模块的最小费用. 分析 ...
- webdriver高级应用- 禁止Chrome浏览器的PDF和Flash插件
#encoding=utf-8 from selenium import webdriver # 导入Options类 from selenium.webdriver.chrome.options i ...
- [办公软件篇][3]windows软件安装
http://www.jeffjade.com/2015/10/19/2015-10-18-Efficacious-win-software/
- hdu2094 stl之set的应用
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- ssh 远程执行绝对路径命令mysqld_multi 报my_print_defaults不存在
通过SSH直接执行远程命令(这种方式会使用Bash的non-interactive + non-login shell模式)找不到命令参考:http://ghoulich.xninja.org/201 ...
- js版本下拉菜单
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 【bzoj4800】[Ceoi2015]Ice Hockey World Championship 折半搜索
题目描述 有n个物品,m块钱,给定每个物品的价格,求买物品的方案数. 输入 第一行两个数n,m代表物品数量及钱数 第二行n个数,代表每个物品的价格 n<=40,m<=10^18 输出 一行 ...