[luogu] P4138 [JOISC2014] 挂饰 01背包DP+贪心

前言

事实告诉我们 不要乱定义变量

传送门 :

思路

CODE

// Problem: P4138 [JOISC2014] 挂饰
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4138
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// Problem: P2738 [USACO4.1]���ʻ�·Fence Loops
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2738
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define px first
#define py second
#define pb push_back
typedef pair<int,int> pii;
int dxy[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
const int N  = 2e3+10,INF = 0x3f3f3f3f;


struct union_find_set{
    int n,p[N];

    void init(int N)
    {
        n = N;
        for(int i=1;i<=n;i++)
        p[i] = i ;
    }

    int find(int x)
    {
        if(p[x]!=x)return p[x] =find(p[x]);
        return p[x];
    }

    bool set_union(int u,int v)
    {
        return (u = find(u)) ==(v == find(v))?false : p[v] = u ;

    }
}ufs;

struct node
{
	int a,b;
}p[N];int node_idx;


int n,m,t;
int f[N];
//表示当还有i个剩余挂钩的时,最大的装饰度

int a[N],b[N];

int pl_ans,pl_sum;


int tb[N],tb_idx;

bool cmp(int a,int b)
{
	return a>b;
	
}
int s[N];

void solve()
{
	cin>>n;
	pl_sum =  1;
	
	for(int i=1;i<=n;i++)
	{
		//挂钩和开心程度
		cin>>a[i]>>b[i];
		
		
		if(a[i] == 0 )
		{
			if(b[i]<= 0)
			continue;
			else//没有挂钩 但是开心的值
			{
				++tb_idx;
				tb[tb_idx] = b[i];
			}
		}else
		{
			if(b[i]>=0)
			{
				pl_sum+=(a[i]-1);
				pl_ans+=b[i];
			}//必拿
			else
			{
				++node_idx;
				p[node_idx] = {a[i]-1,b[i]};
			}
		}
	}
	pl_sum = min(pl_sum,n);
	//总共就n个物品 
	
	for(int i = 0 ;i<= 2000 ;i++)
	f[i] = -INF;
	
	for(int i= 0 ; i<=pl_sum;i++)
	{
		f[i] = 0 ;
	}
	
	for(int i =1;i<=node_idx;i++)
		for(int j = 2000;j>=p[i].a;j--)
		f[j] = max(f[j],f[j-p[i].a] + p[i].b);
		
	for(int i=n-1;i>=0;i--)
	f[i] = max(f[i],f[i+1]);
	
	sort(tb+1,tb+1+tb_idx,cmp);
	
	for(int i=1;i<=2000;i++)
	s[i] = s[i-1]+tb[i];
	
	int ans = 0 ;
	for(int i =0 ;i<=2000;i++)
	{
		ans =max(ans,f[i]+s[i]);
	}
	cout<<ans+pl_ans<<endl;
	
}

int main()
{
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值