Stall Reservations
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 3211 | Accepted: 1154 | Special Judge |
Description
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:
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
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.
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.
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:
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.
题意:
又是母牛的问题,有N只母牛,现在要安排它们挤奶。但是每个母牛都有一个挤奶的时间段,现在要你安排用最少的小隔间给母牛挤奶。
思路:
将所有母牛的挤奶时间按照开始时间升序排序,刚开始想要每次进行一个比较找出最早结束的母牛,但是因为数据过大超时了。后来看别人的算法使用优先队列,第一次用,当做练手了。(ps:优先队列http://blog.csdn.net/zhang20072844/article/details/10286997)设置一个优先队列,以结束时间越早优先级越高,每次比较是否存在比当前奶牛开始时间更早结束的母牛,有就还用那个stall,没有就再添一个stall;
注意点:
1.将元素放入队列是copy操作,之后对于元素的操作不会再影响队列里面的状态;
2.“which includes both times A and B. ”所以挤奶的时间边界不能重叠;
3.cin、cout效率太低,推荐使用scanf、printf,多么痛的领悟~~
样例:
3
1 2
2 3
3 4
——
2
1
2
1
——
4
1 4
2 3
3 5
4 7
——
3
1
2
3
2
代码如下:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAX_N 50010
#define INF 100000000
using namespace std;
struct node{
int s_t;
int e_t;
int pos;
bool operator < (const node &other)const
{
if(this->e_t==other.e_t)
return this->s_t > other.s_t;
return (this->e_t) > (other.e_t);
}
};
priority_queue<node> cList;
int stall[MAX_N];
const bool cmp(node x,node y);
node cows[MAX_N];
int N,sum;
int main()
{
scanf("%d",&N);
for(int i=0;i<N;i++)
{
scanf("%d%d",&cows[i].s_t,&cows[i].e_t);
cows[i].pos=i;
}
sort(cows,cows+N,cmp);sum=1;
stall[cows[0].pos]=1;//仅仅是拷贝,不会再改变
cList.push(cows[0]);
for(int i=1;i<N;i++)
{
if(!cList.empty()&&cList.top().e_t<cows[i].s_t)
{
stall[cows[i].pos]=stall[cList.top().pos];
cList.pop();
}
else
{
sum++;
stall[cows[i].pos]=sum;
}
cList.push(cows[i]);
}
printf("%d\n",sum);
for(int i=0;i<N;i++)
{
printf("%d",stall[i]);
if(i!=N-1)
printf("\n");
}
return 0;
}
const bool cmp(node x,node y)
{
if(x.s_t==y.s_t)
return x.e_t < y.e_t;
return x.s_t < y.s_t;
}