ACM学习历程—HDU 5534 Partial Tree(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534
题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x1)+f(x2)+...+f(xn)的最大值。
首先由于是树,所以有n-1条边,然后每条边连接两个节点,所以总的度数应该为2(n-1)。
此外每个结点至少应该有一个度。
所以r1+r2+...rn = 2n-2。ri >= 1;
首先想到让ri >= 1这个条件消失:
令xi = ri,则x1+x2+...xn = n-2。
然后把所有f的脚标减一。即新f(i) = 原f(i+1)
这样就相当于总和一定,求新f的和的最大值。而且与x的大小顺序无关。
但是到这里利用p(i) = max(p(i), p(i-j)+f(j))的话,需要遍历选取次数、i和j。这个复杂度应该是n^2(n-1)/2,是n^3级别的复杂度。
考虑到0取和不取,虽然不影响i的大小,但是会影响p(i)的大小,而且一个数取了一个0之后,就会少一个数。
于是又有一个消除0的条件:
令f(i) = f(i)-f(0),这样取0的贡献就是0,但是其他值的贡献是与f(0)的差值。
那么最后答案加上原f(0)*n即可。
然后就发现没了0的贡献后,其他值都随便取,而且不会超过n个数。
然后就类似于完全背包一样,利用p(i) = max(p(i), p(i-j)+f(j))即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; const int maxN = ;
int n, f[maxN], p[maxN], ans; void input()
{
scanf("%d", &n);
scanf("%d", &f[]);
for (int i = ; i < n-; ++i)
{
scanf("%d", &f[i]);
f[i] -= f[];
}
ans = f[]*n;
f[] = ;
memset(p, -, sizeof(p));
p[] = ;
} int myMax(int x, int y)
{
if (x == -) return y;
else return max(x, y);
} void work()
{
for (int i = ; i <= n-; ++i)
for (int j = i; j <= n-; ++j)
p[j] = myMax(p[j], p[j-i]+f[i]);
ans += p[n-];
printf("%d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
input();
work();
}
return ;
}
ACM学习历程—HDU 5534 Partial Tree(动态规划)的更多相关文章
- HDU 5534 Partial Tree 完全背包
一棵树一共有2*(n-1)度,现在的任务就是将这些度分配到n个节点,使这n个节点的权值和最大. 思路:因为这是一棵树,所以每个节点的度数都是大于1的,所以事先给每个节点分配一度,答案 ans=f[1] ...
- hdu 5534 Partial Tree 背包DP
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- 2015ACM/ICPC亚洲区长春站 H hdu 5534 Partial Tree
Partial Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU - 5534 Partial Tree(每种都装的完全背包)
Partial Tree In mathematics, and more specifically in graph theory, a tree is an undirected graph in ...
- ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)
Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...
- HDU 5534 Partial Tree (完全背包变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意: 给你度为1 ~ n - 1节点的权值,让你构造一棵树,使其权值和最大. 思路: 一棵树上 ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- hdu 5534 Partial Tree(完全背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5534 题解:这题一看有点像树形dp但是树形dp显然没什么思路.然后由于这里的约束几乎没有就 ...
随机推荐
- IOS中UITableView异步加载图片的实现
本文转载至 http://blog.csdn.net/enuola/article/details/8639404 最近做一个项目,需要用到UITableView异步加载图片的例子,看到网上有一个E ...
- Python 字符串拼接、格式化输出、深浅复制
拼接:"+"号(同类型可拼接) >>>li = [1,2] >>>li + li [1,2,1,2] >>>li*2 [1,2 ...
- 大数据学习系列(1)-- linux之文件系统结构介绍
1./ 根目录 --------- 所有目录挂在其下 2./boot --------- 存放Ubuntu内核和系统启动文件.系统启动时这些文件先被装载. 3./etc --------- 系统的配置 ...
- transport connector和network connector
1 什么是transport connector 用于配置activemq服务器端和客户端之间的通信方式. 2 什么是network connector 用于配置activemq服务器之间的通信方式, ...
- centos修改mysql密码或者进入mysql后解决Access denied for user ''@'localhost' to database 'mysql错误
原因是MySQL的密码有问题 用mysql匿名用户可以进入数据库,但是看不见mysql数据库. 解决办法:具体操作步骤:关闭mysql:# service mysqld stop然后:# mysqld ...
- Django自定义对象转成json字符串
1.定义转换函数:def convert_to_builtin_type(obj): print 'default(', repr(obj), ')' # 把MyObj对象转换成dict类型的对象 d ...
- python数据分析之:时间序列一
在处理很多数据的时候,我们都要用到时间的概念.比如时间戳,固定时期或者时间间隔.pandas提供了一组标准的时间序列处理工具和数据算法. 在python中datetime.datetime模块是用的最 ...
- ubuntu 13.04 设定静态IP
切换到root用户,然后进入/etc/network目录.备份interfaces文件(备份文件是一个好习惯) 下面编辑interfaces文件,添加如下语句: # Assgin static IP ...
- CentOS 6.5 下安装配置GO 1.2.1
步骤1:保持联网状态,命令 # wget http://go.googlecode.com/files/go1.2.linux-amd64.tar.gz 这里下载的是64位,wget这里默认下载到当前 ...
- 【leetcode刷题笔记】Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...