Stall Reservations
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 3063 | Accepted: 1104 | 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.
题意:每个奶牛有固定挤奶的时间段,每个房间最多同时容纳一只奶牛,问至少需要多少房间,以及每只奶牛去哪个房间。
思路:挑战书上贪心部分的题,但我没看出来有多贪心→_→,大概就是先离散化一下数据(但我不清楚不离散化是不是会超时),然后奶牛分别按开始时间和结束时间排序,队列qu中记录还有哪些是空的房间,然后模拟即可。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
int l,r,pos;
}cow1[50010];
bool cmp(node a,node b)
{
return a.l<b.l;
}
struct node2
{
int l,r,pos;
}cow2[50010];
bool cmp2(node2 a,node2 b)
{
return a.r<b.r;
}
int n,m,num1,num2,point[1000010],f[100010];
queue<int> qu;
int main()
{
int i,j,k,maxn;
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
{
scanf("%d%d",&cow1[i].l,&cow1[i].r);
f[i*2-1]=cow1[i].l;
f[i*2]=cow1[i].r;
}
sort(f+1,f+1+n*2);
m=0;maxn=0;
for(i=1;i<=n*2;i++)
if(f[i]!=f[i-1])
{
m++;
point[f[i]]=m;
}
for(i=1;i<=n;i++)
{
cow2[i].l=cow1[i].l=point[cow1[i].l];
cow2[i].r=cow1[i].r=point[cow1[i].r];
cow2[i].pos=cow1[i].pos=i;
}
sort(cow1+1,cow1+1+n,cmp);
sort(cow2+1,cow2+1+n,cmp2);
while(!qu.empty())
qu.pop();
num1=1;num2=1;
for(i=1;i<=m;i++)
{
while(cow1[num1].l==i && num1<=n)
{
if(qu.empty())
{
maxn++;
f[cow1[num1].pos]=maxn;
}
else
{
k=qu.front();
f[cow1[num1].pos]=k;
qu.pop();
}
num1++;
}
while(cow2[num2].r==i && num2<=n)
{
qu.push(f[cow2[num2].pos]);
num2++;
}
}
printf("%d\n",maxn);
for(i=1;i<=n;i++)
printf("%d\n",f[i]);
}
}