Descriptionjoi has n a pendant on the phone, numbered 1 ... N. Joi can put some of them on the phone. Joi's ornaments are somewhat different-some of them have hooks that can hang other pendants. Each pendant is either hung directly on the phone or hung on the hook of the other pendant. There are up to 1 hooks hanging directly on the phone. In addition, each pendant has a joy value to be obtained during installation, expressed as an integer. If Joi hates a pendant, the joy of the pendant is a negative number. Joi wants to maximize the sum of the joy values of all ornaments. Note that you do not have to hang all the hooks on the pendant, and you can do it without hanging it. Input first line an integer n, which represents the number of ornaments. Next N lines, line I (1<=i<=n) has two space-delimited integer ai and bi, which indicates that the pendant i has an AI hook, which will get the joy value of bi after installation. An integer on the output line indicating the maximum value of the attached pendant on the phone, sample Input
5 0 4 2 -21 -10103
Sample Output
5
Hint Pendant 2 directly on the phone, and then the pendant 1 and the pendant 5 are hung on the pendant 2 two hooks, you can get the maximum joy value 4-2+3=5.
1<=n<=2000
0<=ai<=n (1<=i<=n)
-10^6<=bi<=10^6 (1<=i<=n)
Source
JOI 2013~2014 Spring Training Athletic 4 by POPOQQQ
Solution
Obviously, it's a motion problem.
To satisfy the enumeration, first sort the pendant with the number of hooks as the first keyword
Set F[I][J] for the first 1~i after considering the maximum joy of J hooks, transfer a bit of the line
#include <stdio.h>#include<memory.h>#include<algorithm>#defineMAXN 2010#defineMaxbuf 1<<22#defineRG Register#defineInline __inline__ __attribute__ ((always_inline))#defineBlue () {(s = = t&& (t= (s=b) +fread (B,1,maxbuf,stdin), S = = T))? 0:*s++}#defineDMin (a) < (b) ( A):(B))#defineDMax (a) > (b) ( A):(B))Charb[maxbuf],*s=b,*t=b;template<classType>inlinevoidRin (RG Type &x) {x=0; RgintC=blue (); RgBOOLb=false; for(; c< -|| C> $; C=Blue ())if(c = = $) b=true; for(; c> -&&c< -; C=Blue ()) x= (x<<1) + (x<<3) +c- -; if(b) x=-x;}intn,f[maxn][maxn],ans=-~0U<<1;structjuery{intlink_num,happiness_brought_by; BOOL operator< (ConstJuery &other)Const { returnLink_num >Other.link_num; }}A[MAXN];intMain () {Rin (n); for(RGintI=1; i<=n; i++) Rin (A[i].link_num), Rin (a[i].happiness_brought_by); Std::sort (A+1, A +1+N); memset (f,-0x3f,sizeoff); f[0][1]=0; for(RGintI=1; i<=n; i++) for(RGintj=0; j<=n; J + +) F[i][j]=dmax (f[i-1][j],f[i-1][dmax (0, J-a[i].link_num) +1]+a[i].happiness_brought_by); for(RGintI=0; i<=n; i++) ans=DMax (Ans,f[n][i]); printf ("%d\n", ans); return 0;}
[bzoj4247] [Pendant] (Dynamic rule + sort)