Supermarket POJ - 1456 (贪心+并查集)

先将n个物品按价值降序排个序,从头扫到尾,对于每一个物品i,判断能不能在<=di的最大时间点卖掉。

#include
#include
#include
#include
using namespace std;
const int maxn=1e4+10;
struct node
{
    int p,d;
}th[maxn];
int fa[maxn];//fa[i]表示的是i时刻的状态,-1为未被占用
bool cmp(const node&a,const node&b)
{
    return a.p>b.p;
}
int ifind(int x)
{
    if(fa[x]==-1)
        return x;
    return fa[x]=ifind(fa[x]);  
}
int main() 
{
    int n,ans;
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        memset(fa,-1,sizeof(fa));//
        for(int i=0;i0) //t从1开始,t=0,则是非法的时间点
            {
                ans+=th[i].p;
                fa[t]=t-1; //这个语句表示t时间点的下一个合法时间点要从t-1开始找
            }

        }
        printf("%d\n",ans);
    }
    return 0;
}

 

你可能感兴趣的:(贪心,并查集)