Bzoj 4247: Pendant (DP)

Source: Internet
Author: User

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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.