4247: Pendant Time Limit:10 Sec Memory limit:256 MB
submit:1230 solved:497
[Submit] [Status] [Discuss] Description Joi 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 that represents the maximum value of the total number of ornaments connected on the phone
Sample Input 5 0 4 2-2 1-1 0 1 0 3 sample Output 5
DP[I][J] for the top I pendant, there is currently the maximum joy value of J hooks
DP[I][J] = max (Dp[i-1][j], Dp[i-1][max (j-s[i].x+1, 1)]+s[i].y)
Where s[i].x indicates how many hooks are in the first pendant, S[I].Y is the joy value
Initialize dp[0][1] = 0, the other is a negative number as small as possible
Why is Max (j-s[i].x+1, 1) above, because the hooks don't have to run out
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef struct RES
{
int x, y;
BOOL operator < (const Res &b) Const
{
if (x>b.x)
return 1;
return 0;
}
} Res;
Res s[2005];
int dp[2005][2005];
int main (void)
{
int n, I, J, ans;
scanf ("%d", &n);
for (i=1;i<=n;i++)
scanf ("%d%d", &s[i].x, &s[i].y);
Sort (s+1, s+n+1);
Memset (DP, -126, sizeof (DP));
Ans = dp[0][1] = 0;
for (i=1;i<=n;i++)
{
for (j=0;j<=n;j++)
dp[i][j] = max (Dp[i-1][j], Dp[i-1][max (j-s[i].x+1, 1)]+ S[I].Y);
}
for (i=0;i<=n;i++)
ans = max (ans, dp[n][i]);
printf ("%d\n", ans);
return 0;
}