简单区间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. 常用的css

    产品鼠标经过加边框效果 .productsCol:hover { box-shadow: 0 0 0 3px #333333 inset; transition: all 0.2s ease 0s; ...

  2. OpenCV:二值图像连通区域分析与标记算法实现

    http://blog.csdn.net/cooelf/article/details/26581539?utm_source=tuicool&utm_medium=referral Open ...

  3. hdu_1513_Palindrome(LCS+滚动数组)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意:给你一个字符串,问你最少插入多少个字符使其为回文字符. 题解:将字符串倒着保存,然后求一下 ...

  4. Codeforce#354_B_Pyramid of Glasses(模拟)

    题目连接:http://codeforces.com/contest/676/problem/B 题意:给你一个N层的杯子堆成的金字塔,倒k个杯子的酒,问倒完后有多少个杯子的酒是满的 题解:由于数据不 ...

  5. what a malloc has to do

    1) Allocate a chunk of memory big enough to satisfy the request, and return a pointer to it.2) Remem ...

  6. CSS概要

    CSS概要 laiqun@msn.cn Contents 1. css的引入 2. css的选择器及效果图 3. css 盒模型 4. css 浮动 4.1. 浮动的作用: 4.2. 浮动的影响: 5 ...

  7. 微信支付WxpayAPI_php_v3(一)sdk简介与错误修改

    经过断断续续将近一周的时间终于把微信支付调通了. 这里总结一下,算是给后来者有个指引.少踩坑!!!! 开发语言:php5.5 语言框架:laravel5.2 微信sdk:WxpayAPI_php_v3 ...

  8. SQL语句创建access表

    CREATE TABLE Persons(ID AutoIncrement primary key,Id_P int NOT NULL,LastName varchar(255) NOT NULL,s ...

  9. linux fork()函数

    C语言编程创建函数fork() 执行解析 | 浏览:1842 | 更新:2013-04-22 15:12 | 标签:c语言 概述 最近在看进程间的通信,看到了fork()函数,虽然以前用过,这次经过思 ...

  10. 在PL/SQL/sqlplus客户端 中如何让程序暂停几秒钟

    1. how to check procedure exist: SQL> conn oper/oper123Connected.SQL> desc dbms_lock;PROCEDURE ...