题目链接: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(动态规划)的更多相关文章

  1. HDU 5534 Partial Tree 完全背包

    一棵树一共有2*(n-1)度,现在的任务就是将这些度分配到n个节点,使这n个节点的权值和最大. 思路:因为这是一棵树,所以每个节点的度数都是大于1的,所以事先给每个节点分配一度,答案 ans=f[1] ...

  2. hdu 5534 Partial Tree 背包DP

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  3. 2015ACM/ICPC亚洲区长春站 H hdu 5534 Partial Tree

    Partial Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  4. HDU - 5534 Partial Tree(每种都装的完全背包)

    Partial Tree In mathematics, and more specifically in graph theory, a tree is an undirected graph in ...

  5. ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)

    Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...

  6. HDU 5534 Partial Tree (完全背包变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意: 给你度为1 ~ n - 1节点的权值,让你构造一棵树,使其权值和最大. 思路: 一棵树上 ...

  7. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

  8. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  9. hdu 5534 Partial Tree(完全背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5534 题解:这题一看有点像树形dp但是树形dp显然没什么思路.然后由于这里的约束几乎没有就 ...

随机推荐

  1. 九度OJ 1325:Battle Over Cities(城市间的战争) (并查集)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:376 解决:132 题目描述: It is vitally important to have all the cities connect ...

  2. redis持久化AOF详细操作步骤

    1.切换到redis目录下面,创建文件 s3-redis.conf 2.编辑文件s3-redis.conf 3.终止当前redis服务端 4.登录redis客户端失败,说明服务端已停止 5.重启red ...

  3. C# 汉字编码GB2312转换

    功能界面 源码: using System; using System.Collections.Generic; using System.ComponentModel; using System.D ...

  4. STM32 ~ MDK环境下调试程序 HardFault_Handler 相关

    STM32出现HardFault_Handler故障的原因主要有两个方面: 1.内存溢出或者访问越界.这个需要自己写程序的时候规范代码,遇到了需要慢慢排查. 2.堆栈溢出.增加堆栈的大小. 出现问题时 ...

  5. 改善程序与设计的55个具体做法 day9

    条款23:宁以non-member.non-friend替换member函数 即 以非成员函数 非友元函数 替换成员函数. 直观上,面向对象应该尽可能的封装,封装数据.封装操作等等,所以这个条款可能有 ...

  6. jQuery设计理念

    jQuery设计理念 引用百科的介绍: jQuery是继prototype之后又一个优秀的Javascript框架.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1. ...

  7. 使用cocoaPods import导入时没有提示的解决办法

    1.选择target(就是左边你的工程target)—— BuildSettings —— search Paths 下的 User Header Search Paths(如图所示:)    2.双 ...

  8. 跨平台(I版到K版)迁移实践总结

           所谓跨平台迁移,对于了解openstack冷迁移过程的同学来说,其实就是手动去执行冷迁移的代码行为,当然像我这种抵制体力劳动的人,肯定会想写脚本去跑,即使不会也要边学边用. 迁移并非想象 ...

  9. phpmyadmin mysqlnd cannot connect to

    mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. Please use an administra ...

  10. RSA加密方法java工具类

    package com.qianmi.weidian.common.util; import java.io.ByteArrayOutputStream; import java.security.K ...