4247: Pendant
Time Limit:1 Sec
Memory limit:256 MB
Topic Connection http://www.lydsy.com/JudgeOnline/problem.php?id=4247
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. Outputoutputs a single integer that represents the maximum value of the total number of ornaments connected on the phoneSample INPUT5
0 4
2-2
1-1
0 1
0 3Sample Output5Hint 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)
Test instructions
Exercises
Knapsack problem, Dp[i][j] says that when considering item I, there are still J hooks left.
Note that the hooks are likely to become negative and then be added positive if they are not sorted by how many hooks are sorted.
Code copied from: http://blog.csdn.net/creationaugust/article/details/48133509
Code:
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<bitset>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 4051#defineMoD 10007#defineEPS 1e-9intNum;//const int INF=0X7FFFFFFF; //нчоч╢сConst intinf=0x3f3f3f3f; inline ll read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************structnode{intx, y;};BOOLCMP (node A,node b) {returnA.x>b.x;} Node A[maxn];ll DP[MAXN>>1][MAXN];intMain () {intn=read (); for(intI=0; i<=n;i++) dp[0][i]=dp[i][n+1]=-inf; for(intI=1; i<=n;i++) a[i].x=read (), a[i].y=read (); Sort (a+1, A +1+n,cmp); ll ans=0; dp[0][1]=0; for(intI=1; i<=n;i++) { for(intj=0; j<=n;j++) {Dp[i][j]=max (dp[i-1][max (j-a[i].x,0)+1]+a[i].y,dp[i-1][j]); } } for(intI=0; i<=n;i++) ans=Max (ans,dp[n][i]); printf ("%d\n", ans);}
Bzoj 4247 Pendant Backpack DP