POJ1456(通用版二S题) 贪心

原本想用排序写,由于最近优先队列感觉很好用,就直接用了优先队列

贪心策略和之前有一道补作业的题相似,取最大值往后向前安排即可》

#include<cstdio>
#include<queue>
using namespace std;
struct node
{
    int pro;
    int dead;
    bool operator<(const node &a) const
    {
        if (pro==a.pro) return dead>a.dead; else return pro<a.pro;
    }
};
int main()
{
    int n;
    while (scanf("%d",&n)!=EOF)
    {
        int i,sum=0,flag[10010]={0},j;
        priority_queue<node>q;
        struct node a;
        for (i=0;i<=n-1;i++) {scanf("%d%d",&a.pro,&a.dead);q.push(a);}
        for (i=0;i<=n-1;i++)
            {
                a=q.top();
                for (j=a.dead;j>=1;j--)
                if (!flag[j]) {flag[j]=1;sum+=a.pro;break;}
                q.pop();
            }
        printf("%d\n",sum);
    }

}

猜你喜欢

转载自blog.csdn.net/parkerljc/article/details/79363346