简单区间DP。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; const int maxn = ;
int n;
int a[maxn], sum[maxn], dp[maxn][maxn]; int main()
{
while (~scanf("%d", &n))
{
sum[] = ;
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = ; i <= n; i++) sum[i] = sum[i - ] + a[i]; memset(dp, , sizeof dp); for (int i = ; i <= n; i++)
{
for (int j = ; j+i- <= n; j++)
{
int st = j, en = j + i - ;
int ans = 0x7FFFFFFF;
for (int k = st; k <= en; k++)
ans = min(dp[st][k - ] + dp[k + ][en] + sum[en] - sum[st - ] - a[k],ans);
dp[st][en] = ans;
}
}
printf("%d\n", dp[][n]);
}
return ;
}

UVA 10304 Optimal Binary Search Tree的更多相关文章

  1. uva 10304 - Optimal Binary Search Tree 区间dp

    题目链接 给n个数, 这n个数的值是从小到大的, 给出个n个数的出现次数. 然后用他们组成一个bst.访问每一个数的代价是这个点的深度*这个点访问的次数. 问你代价最小值是多少. 区间dp的时候, 如 ...

  2. ITA 15.5 Optimal binary search trees

    p400 页最后一段 When j >= i , we need to select a root kr from among ki ... kj and then make an optima ...

  3. Optimal binary search trees

    问题 该问题的实际应用 Suppose that we are designing a program to translate text from English to French. For ea ...

  4. UVA 1264 - Binary Search Tree(BST+计数)

    UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...

  5. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  6. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  7. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  8. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  9. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

随机推荐

  1. ubuntu如何实现访问实际网络中windows共享文件夹

    方法一: 首先在建立一个挂载目录. sudo mkdir /mnt/share 然后就把共享目录持载进去. 服务器:192.168.6.84 共享名:gg 用户名:administrator 密 码: ...

  2. 取出parentid为null的顶级栏目 等号改为 is null 避免null当做字符串,

    mysql中查询字段为null或者不为null 在mysql中,查询某字段为空时,不可用等号 = null, 而是 is null,不为空则是 is not null    select * from ...

  3. asp.net无法触发asp控件的后台方法

    前台代码: <asp:Button ID="btnFinish" runat="server" Text="完成" Font-Size ...

  4. 转 如何不耍流氓的做运维之——SHELL脚本

    家都是文明人,尤其是做运维的,那叫一个斯文啊.怎么能耍流氓呢?赶紧看看,编写 SHELL 脚本如何能够不耍流氓. 下面的案例,我们以 MySQL 数据库备份 SHELL 脚本的案例来进行阐述. 不记录 ...

  5. php 常用的JS

      function doit(){ var sel_val=$("#type").val(); if (sel_val=='') { $("#bigClassId1&q ...

  6. Apache 的常见问题

    Apache "No services installed"问题的处理以及Apache提示 the requested operation has failed而无法启动 安装完 ...

  7. jquery 限制字数 显示输入字数 插件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 项目中的BaseServlet

    BaseServlet代码: import java.io.IOException; import java.lang.reflect.Method; import javax.servlet.Ser ...

  9. hack,不同的IE浏览器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. http://www.iteye.com/job/topic/1133159

    Lucene 的索引体系是一个写独占,读共享的结构,这意味着,我们在使用多线程进行添加索引时,性能并不会得到明显的提升,所以任何时刻只能有一个线程对索引进行写 入操作,而保障这个操作的安全性则是来自于 ...