POJ

来源:互联网 发布:百度关键词排名软件 编辑:程序博客网 时间:2024/06/05 15:06

题意:

      求给每个时间段的牛分配牛棚,时间不冲突的牛可以在一个牛棚。用优先队列和贪心。

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows. 

Help FJ by determining:
  • The minimum number of stalls required in the barn so that each cow can have her private milking period
  • An assignment of cows to these stalls over time
Many answers are correct for each test dataset; a program will grade your answer.
Input
Line 1: A single integer, N 

Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.
Output
Line 1: The minimum number of stalls the barn must have. 

Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.
Sample Input
51 102 43 65 84 7
Sample Output
412324
Hint
Explanation of the sample: 

Here's a graphical schedule for this output: 

Time     1  2  3  4  5  6  7  8  9 10Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..Stall 3 .. .. c3>>>>>>>>> .. .. .. ..Stall 4 .. .. .. c5>>>>>>>>> .. .. ..
Other outputs using the same number of stalls are possible.


#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<queue>

#define maxn 60000

using namespace std;

typedef pair<int ,int>k;

struct node{

    int s,t;

    int d;

    int pos,pos1;

}p[maxn];

bool cmp(node a,node b)

{

    if(a.s==b.s)

        return a.t<b.t;

    return a.s<b.s;

}

bool operator < (constnode &a,constnode &b)

{

    if(a.t==b.t)

        return a.s>b.s;

    return a.t>b.t;

}

int main()

{

    int use[maxn];

    memset(use,0,sizeof(use));

    priority_queue<node>que;

    int n;

    scanf("%d",&n);

    for(int i=0;i<n;i++)

    {

        scanf("%d%d",&p[i].s,&p[i].t);

        p[i].pos=i;

        p[i].d=p[i].t-p[i].s;

        p[i].pos1=i;

    }

    sort(p,p+n,cmp);

     int ans=1;

    use[p[0].pos]=1;

    que.push(p[0]);

    for(int i=1;i<n;i++)

    {

        if(que.top().t<p[i].s&&!que.empty())

        {

            use[p[i].pos]=use[que.top().pos];

            que.pop();

        }

        else

        {

            ans++;

            use[p[i].pos]=ans;

        }

        que.push(p[i]);

    }

  //  sort(p,p+n,cmp3);

    printf("%d\n",ans);

    for(int i=0;i<n;i++)

        printf("%d\n",use[i]);

    return 0;

}