POJ 3190 Stall Reservations

优先级队列。

对时间的起始时间排个序一个一个看,机器按照结束时间放入优先级队列中维护即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;

const int maxn=50000+10;
int n;
struct X
{
    int L,R,id,ans;
} s[maxn];

struct U
{
    int t,id;
    U(int a,int b)
    {
        t=a;
        id=b;
    }
    bool operator < (const U &a) const
    {
        return t>a.t;
    }
};

bool cmp(const X&a,const X&b)
{
    return a.L<b.L;
}

bool cmp2(const X&a,const X&b)
{
    return a.id<b.id;
}


int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&s[i].L,&s[i].R);
            s[i].id=i;
        }
        sort(s+1,s+1+n,cmp);
        priority_queue<U>Q;
        int ans=1;
        U f(s[1].R,1);
        s[1].ans=1;
        Q.push(f);
        for(int i=2; i<=n; i++)
        {
            if(Q.top().t<s[i].L)
            {
                U f=Q.top(); Q.pop();
                s[i].ans=f.id; f.t=s[i].R; Q.push(f);
            }
            else
            {
                ans++; s[i].ans=ans;
                U f(s[i].R,ans); Q.push(f);
            }
        }

        sort(s+1,s+1+n,cmp2);

        printf("%d
",ans);
        for(int i=1; i<=n; i++)
            printf("%d
",s[i].ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5322716.html