首页 > 其他 > 详细

Supermarket POJ - 1456 贪心+并查集

时间:2020-02-01 20:09:00      阅读:70      评论:0      收藏:0      [点我收藏+]
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5;
struct edge{
    int w;
    int deadline;
}e[N];
bool cmp(edge a,edge b)
{
    return a.w>b.w;
}
int f[N];
int find(int x)
{
    if(f[x]!=x)
        f[x]=find(f[x]);
    return f[x];
}
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=1;i<=N;i++)
            f[i]=i;
        for(int i=0;i<n;i++)
            cin>>e[i].w>>e[i].deadline;
        sort(e,e+n,cmp);
        int res=0;
        for(int i=0;i<n;i++)
        {
            //   刚开始占用了第x天  p[x]=x-1
            //  占用第x-1天   p[x-1]=x-2
            //此时再占用x   p[x]=x-1
            //p[x-1]=x-2
            //p[x-2]=x-2  占用 
            int x=find(e[i].deadline);
            if(x>0)
            {
                //该商品已经用了这一天,所以其他商品要用的话只能提前,所以-1
                f[x]=x-1;
                res+=e[i].w;
            }
        }
        cout<<res<<endl;
    }
}

 

Supermarket POJ - 1456 贪心+并查集

原文:https://www.cnblogs.com/QingyuYYYYY/p/12249856.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!