Title Link:http://acm.hdu.edu.cn/showproblem.php?pid=5534
The main idea is to give n nodes, let the latter make up a tree, assuming that the degree of each node is r1, R2, ... rn, f (x1) +f (x2) +...+f (xn) the maximum value.
First because it is a tree, so there are n-1 Edge, and then each side connected two nodes, so the total degree should be 2 (n-1).
In addition, each node should have at least one degree.
So r1+r2+...rn = 2n-2. ri >= 1;
First think of the condition that makes ri >= 1 disappear:
Make XI = ri, then x1+x2+...xn = n-2.
Then subtract all of the F's foot marks by one. i.e. New f (i) = original f (i+1)
This is equivalent to the sum of certain, novelty F and the maximum value. and is independent of the size order of X.
But to use p (i) = max (P (i), P (i-j) +f (j)) , it is necessary to traverse the selection,i and J. This complexity should be n^2 (n-1)/2, which is The complexity of the n^3 level.
Considering that 0 is taken and not taken, although it does not affect the size of I , it affects the size of p (i) , and a number takes a 0 After that, there will be one less number.
Then there is a condition to eliminate 0 :
Make f (i) = f (i)-f (0), so that the contribution of 0 is 0, but the contribution of the other values is with f ( 0) of the differential value.
Then the final answer plus the original f (0) *n can be.
Then we find that no 0 of the contribution, the other values are randomly taken, and no more than n number.
Then just like a full backpack, use p (i) = max (P (i), P (i-j) +f (j)) .
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<Set>#include<map>#include<queue>#include<string>#defineLL Long Longusing namespacestd;Const intMAXN =2050;intN, F[maxn], P[maxn], ans;voidinput () {scanf ("%d", &N); scanf ("%d", &f[0]); for(inti =1; I < n1; ++i) {scanf ("%d", &F[i]); F[i]-= f[0]; } ans= f[0]*N; f[0] =0; Memset (P,-1,sizeof(p)); p[0] =0;}intMymax (intXinty) { if(x = =-1)returny; Else returnMax (x, y);}voidWork () { for(inti =0; I <= N-2; ++i) for(intj = i; J <= N-2; ++j) P[j]= Mymax (P[j], p[j-i]+F[i]); Ans+ = p[n-2]; printf ("%d\n", ans);}intMain () {//freopen ("test.in", "R", stdin); intT; scanf ("%d", &T); for(intTimes =0; Times < T; ++Times ) {input (); Work (); } return 0;}View Code
ACM Learning process-hdu 5534 Partial Tree (Dynamic planning)