Stall Reservations(貪心)

 

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

5
1 10
2 4
3 6
5 8
4 7

Sample Output

4
1
2
3
2
4

Hint

Explanation of the sample:

Here's a graphical schedule for this output:

Time     1  2  3  4  5  6  7  8  9 10

Stall 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 <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef __int64 ll;
const int idata=1e5+5;

int n,m,t,_;
int i,j,k;
int cnt,num;
int minn,maxx;
struct Node
{
    int st,e;
    int id;
    const bool operator<(const Node & b)
    const{
        return e>b.e;//結束時間早的在隊首
    }
}node[idata];

priority_queue<Node>q;

bool cmp(Node a,Node b)
{
    return a.st==b.st ? a.e<b.e : a.st<b.st;
}
int ans[idata];
void clear()
{
    priority_queue<Node>temp;
    swap(temp,q);
    return ;
}
int main()
{
    cin.tie(0);
    iostream::sync_with_stdio(false);
    while(cin>>n)
    {
        Node temp;
        for(i=1;i<=n;i++){
            cin>>node[i].st>>node[i].e;
            node[i].id=i;
        }
        sort(node+1,node+1+n,cmp);
        clear();

        num=0;
        q.push(node[1]);
        ans[ node[1].id ]=++num;
        for(i=2;i<=n;i++){
            if(!q.empty() && q.top().e<node[i].st ){
                temp=q.top();
                q.pop();
                ans[ node[i].id ]=ans[ temp.id ];
            }
            else ans[ node[i].id ]=++num;
            q.push(node[i]);
        }
        cout<<num<<endl;
        for(i=1;i<=n;i++){
            cout<<ans[i]<<endl;
        }
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章