poj 1456 并查集&贪心

题意是,告诉你每个货物的利润和截止日期,让你求在最后一件货物过期前的最大利润。

分析:要想求最大利润,那么每一时间单位都只需要卖出最大利润的货物就可以,如果冲突,就是要并查集来快速查找上一个不矛盾点,来放置。

代码:

#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
using namespace std;

const int maxn=10010;
struct Node
{
    int p,d;
}node[maxn];
int root[maxn];

bool cmp(Node a,Node b)
{
    return a.p>b.p;
}

int find(int x)
{
    if(root[x]==-1) return x;
    else return root[x]=find(root[x]);
}

int main()
{
    int m;
    while(scanf("%d",&m)!=EOF)
    {
        memset(root,-1,sizeof root);
        for(int i=0;i<m;i++)
        {
            scanf("%d %d",&node[i].p,&node[i].d);
        }
        sort(node,node+m,cmp);
        long long sum=0;
        for(int i=0;i<m;i++)
        {
            int t=find(node[i].d);
            if(t>0)
            {
                sum+=node[i].p;
               root[t]=t-1;
            }
        }
        printf("%lld\n",sum);
    }
    //system("pause");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值