poj3190(贪心,优先队列)

来源:互联网 发布:ds是什么意思网络用语 编辑:程序博客网 时间:2024/05/29 16:37
#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>using namespace std;struct p{    int x;    int y;    int z;    friend bool operator<(p n1,p n2)//维护一个优先队列    {        return n1.y>n2.y;    }} a[50010];bool cmp(p x,p y){    if(x.x==y.x)        return x.y<y.y;    return x.x<y.x;//按照开始时间从小到大排序}int main(){    int t;    int s[50010];    while(~scanf("%d",&t))    {        memset(s,0,sizeof(s));        for(int i=0; i<t; i++)        {            scanf("%d%d",&a[i].x,&a[i].y);            a[i].z=i;//记录位置        }        sort(a,a+t,cmp);        priority_queue<p>Q;        Q.push(a[0]);        s[a[0].z]=1;//记录使用的机器序号        int sum=2;        for(int i=1; i<t; i++)        {            if(Q.top().y>=a[i].x)                s[a[i].z]=sum++;            else            {                s[a[i].z]=s[Q.top().z];                Q.pop();            }            Q.push(a[i]);        }        printf("%d\n",sum-1);        for(int i=0; i<t; i++)            printf("%d\n",s[i]);    }    return 0;}

0 0
原创粉丝点击