HDU - 5534 Partial Tree(每种都装的完全背包)
Partial Tree
You find a partial tree on the way home. This tree has nn nodes but lacks of n−1n−1 edges. You want to complete this tree by adding n−1n−1edges. There must be exactly one path between any two nodes after adding. As you know, there are nn−2nn−2 ways to complete this tree, and you want to make the completed tree as cool as possible. The coolness of a tree is the sum of coolness of its nodes. The coolness of a node is f(d)f(d), where ff is a predefined function and dd is the degree of this node. What's the maximum coolness of the completed tree?
InputThe first line contains an integer TT indicating the total number of test cases.
Each test case starts with an integer nn in one line,
then one line with n−1n−1 integers f(1),f(2),…,f(n−1)f(1),f(2),…,f(n−1).
1≤T≤20151≤T≤2015
2≤n≤20152≤n≤2015
0≤f(i)≤100000≤f(i)≤10000
There are at most 1010 test cases with n>100n>100.OutputFor each test case, please output the maximum coolness of the completed tree in one line.Sample Input
2
3
2 1
4
5 1 4
Sample Output
5
19 因为每种装的范围为【1,+无穷】,然而分组背包的三次方会T,所以要想办法将其转化为完全背包模型。
因为每个点都至少有一度,所以我们预先放入n个一度,本来要放的2×n-2度现在只剩n-2度。
每当再次放入一个x度时,他的贡献为x-1度,在放入的同时减掉之前预先放好的一度。这样便保证了每个点至少有一度。
#include<bits/stdc++.h>
#define MAX 2018
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll; int a[MAX];
ll dp[MAX]; int main()
{
int t,n,m,i,j,k;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%d",&a[i]);
}
memset(dp,-INF,sizeof(dp));
dp[]=;
for(i=;i<n;i++){
for(j=i-;j<=n-;j++){
dp[j]=max(dp[j],dp[j-(i-)]+a[i]-a[]);
}
}
printf("%I64d\n",dp[n-]+n*a[]);
}
return ;
}
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 (完全背包变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意: 给你度为1 ~ n - 1节点的权值,让你构造一棵树,使其权值和最大. 思路: 一棵树上 ...
- ACM学习历程—HDU 5534 Partial Tree(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...
- hdu 5534 Partial Tree(完全背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5534 题解:这题一看有点像树形dp但是树形dp显然没什么思路.然后由于这里的约束几乎没有就 ...
- 2015长春 HDU 5534 Partial Tree
题意:有n个结点,n-1条边,现在要把这n个结点连成一棵树,给定了f(i),表示度为i的结点的价值是f(i).现在问如何连能够使得Σf(i)的值最大. 思路:每个点至少一个度,所以可分配的度数为n-2 ...
- HDU 5534 Partial Tree
2015 ACM/ICPC 长春现场赛 H题 完全背包 #include<cstdio> #include<cstring> #include<cmath> #in ...
- H - Partial Tree HDU - 5534 (背包)
题目链接: H - Partial Tree HDU - 5534 题目大意:首先是T组测试样例,然后n个点,然后给你度数分别为(1~n-1)对应的不同的权值,然后问你在这些点形成树的前提下的所能形 ...
随机推荐
- 点聚-weboffice 6.0 (二)
1.修订操作 //设置当前操作用户 function SetUserName() { try{ var webObj=document.getElementById("WebOffice1& ...
- 我的Java开发学习之旅------>计算从1到N中1的出现次数的效率优化问题
有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数.比如f(1)=1:f(13)=6,问一个最大的能满足f(n)=n中的n是什么? 例如:f(13)=6, 因为1, ...
- 【windows】更改最大动态端口数
最近业务遇到一个奇怪的问题,一台iis服务器,居然报端口不足的错误,分析应该是服务器可用的动态端口数不够了,windows默认的动态端口范围为:1024-5000,也就是最多3977个动态端口可用,如 ...
- Redis——慢查询分析
核心知识点: 1.什么是慢查询? 2.客户端执行一条命令的步骤? 3.阈值和慢查询日志的设置? 4.慢查询日志的操作命令:slowlog get.slowlog len.slowlog reset. ...
- session,cookie的理解(总结)
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...
- (linux)LED子系统
数据结构(/include/linux/leds.h) enum led_brightness { LED_OFF = 0, LED_HALF = 127, LED_FULL = 255, }; le ...
- 程序连接Oracle数据库出现未找到提供程序.该程序可能未正确安装错误提示
好不容易使用plsql可以成功连上数据库了,应用程序连接数据库却出现了问题 其实解决这个问题也简单: 1. 查看oracle安装目录下的BIN目录,E:\app\Administrator\prod ...
- java语言中Object对象的hashCode()取值的底层算法是怎样实现的
Java语言中,Object对象有个特殊的方法:hashcode(), hashcode()表示的是JVM虚拟机为这个Object对象分配的一个int类型的数值,JVM会使用对象的hashcode值来 ...
- 为什么在实际的 kaggle 比赛中 gbdt 和 random forest 效果非常好?
https://www.zhihu.com/question/51818176/answer/127637712
- 创建Django博客的数据库模型
声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 blog最主要的功能就是展示我们写的文章 ...