Luo Gu P2340 cows exhibition solution to a problem

description:

Bessie the right to choose to participate in the exhibition which cows. Since the negative effects of negative IQ or intelligence Chamber of Commerce, so do not expect a show cow Bessie IQ of less than zero, or the EQ and less than zero. Under two conditions are met, she hoped the exhibition of cow's IQ and EQ and the bigger the better, please help Bessie find this maximum.

solution:

This question can think of to do with a backpack.

Because the two providers is relatively equivalent, so we set up as a "price", as a "value" on the line.

This positive and negative, respectively, based on the array index and the value within the index to confirm whether it can update the answer.

But there is a downside is the index can not be negative

But this is also simple, as long as they unify plus a large value on the line.

Note that this mess can not add, because this is the time to be counted backpack complexity, plus 400 * 1000 = 400,000 on the line.

Then Los goo good data metaphysics, read the array can open a small four-fold A ...

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int s[105],f[105];
int dp[900005];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&s[i],&f[i]);
	}
	memset(dp,-0x3f,sizeof(dp));
	dp[400000]=0;
	for(int i=1;i<=n;i++)
	{
		if(s[i]>=0)
		for(int j=800000;j>=s[i];j--)
		{
			dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
		}
		else
		{
			for(int j=0;j<=800000+s[i];j++)
			{
				dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
			}
		}
	}
	int ans=0;
	for(int i=400000;i<=800000;i++)
	{
		if(dp[i]>=0)
		{
			ans=max(ans,dp[i]+i-400000);
		}
	}
	printf("%d\n",ans);
	return 0;
} 
Published 376 original articles · won 157 Like · views 70000 +

Guess you like

Origin blog.csdn.net/kkkksc03/article/details/102093788