poj 1456 Supermarket 贪心 并查集优化

来源:互联网 发布:电脑版电视直播软件 编辑:程序博客网 时间:2024/05/22 07:56

按价值排序后由大到小加进结果,贪心的放在最后的时间里如果某个时间已经占满往前放这时要用并查集维护这天前面没有占满的最后一天

#include<cstdio>#include<algorithm>#include<cstring>#include<cstdlib>#include<queue>#define maxn 10015using namespace std;int n;struct node{    int mo,ti;}no[maxn];int fa[maxn];int getFa(int pre){    if(pre == fa[pre])return pre;    fa[pre] = getFa(fa[pre]);    return fa[pre];}bool cmp(node no1,node no2){    return no1.mo>no2.mo;}int main(){    while(scanf("%d",&n)!=EOF)    {        int time = -1;        for(int i=1;i<=n;i++){ scanf("%d %d",&no[i].mo,&no[i].ti); time = max(time,no[i].ti);}        sort(no+1,no+n+1,cmp);        for(int i=0;i<=time;i++)fa[i] = i;        int sum = 0;        for(int i=1;i<=n;i++)        {            int pre = getFa(no[i].ti);            if(pre)            {               fa[pre] = pre-1;               sum+=no[i].mo;            }        }        printf("%d\n",sum);    }    return 0;}
0 0
原创粉丝点击