Partial Tree
In mathematics, and more specifically in graph theory, a tree is an undirected graph in which any two nodes are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.
You find a partial tree on the way home. This tree has d is the degree of this node. What's the maximum coolness of the completed tree?
You find a partial tree on the way home. This tree has d is the degree of this node. What's the maximum coolness of the completed tree?
InputThe first line contains an integer n>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=1;i<n;i++){ scanf("%d",&a[i]); } memset(dp,-INF,sizeof(dp)); dp[0]=0; for(i=2;i<n;i++){ for(j=i-1;j<=n-2;j++){ dp[j]=max(dp[j],dp[j-(i-1)]+a[i]-a[1]); } } printf("%I64d\n",dp[n-2]+n*a[1]); } return 0; }