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 ...
随机推荐
- 集群环境(session多服务器共享的方案梳理)
目前业界解决session共享的几种思路,我总结如下: 第一种办法:把原来存储在服务器磁盘上的session数据存储到客户端的cookie中去. 这样子,就不需要涉及到数据共享了.a客户端请求的时候, ...
- Python之简单Socket编程
Socket编程这块儿还是比较重要的,记录一下:实现服务器端和客户端通信(客户端发送系统指令,如ipconfig等,服务器端执行该指令,然后将指令返回结果给客户端再传过去,设置一次最多直接收1024字 ...
- ARM系统调用
参考:Linux异常处理体系结构 linux系统调用表(system call table) Arm Linux系统调用流程详细解析-SWI ARM系统调用是通过SWI异常处理函数实现的,这里简要概 ...
- 原来针对新唐mcu,keil有免费许可
MDK for Nuvoton Cortex-M0/M23:The MDK for Nuvoton Cortex-M0/M23 is a license paid by Nuvoton. It is ...
- LA 4094 WonderTeam 构造
题意: 一共有\(n\)支队伍参加比赛,每两支队伍比赛两场,主客场各一场. 胜场得\(3\)分,平局得1分,败场不得分. 一支得分为\(p\)的队伍的排名\(=\)分数大于\(p\)的队伍数\(+1\ ...
- 认识Function.prototype.call
一.前言 大家先预计一下以下四个函数调用的结果吧! var test = function(){ console.log('hello w ...
- day04 装饰器 迭代器&生成器 Json & pickle 数据序列化 内置函数
回顾下上次的内容 转码过程: 先decode 为 Unicode(万国码 ) 然后encode 成需要的格式 3.0 默认是Unicode 不是UTF-8 所以不需要指定 如果非要转为U ...
- Scala学习-01-变量与类型
Scala运行在jvm之上,可以调用Java类库和与Java框架交互,并将面向对象与面向函数结合在一起. 特点: 1 保留了静态类型检查.安全保障高. 2 函数式编程,更加灵活. 3 运行于jvm之上 ...
- Maven之scope详解
scope的分类 compile(编译范围) 默认就是compile,什么都不配置也就是意味着compile.compile表示被依赖项目需要参与当前项目的编译,当然后续的测试, 运行周期也参与其中, ...
- Emacs 安装与体验
Emacs 安装与体验 下载与安装 首先是下载最新的x64版本的Emacs. 将下载下来的文件解压到你喜欢的磁盘目录下,我选择的是C:\Program Files.放在D盘可能是更好的选择,因 ...