L i n k Link Link
l
u
o
g
u
luogu
luogu
2859
2859
2859
P
O
J
POJ
POJ
3190
3190
3190
D e s c r i p t i o n Description Description
约翰的N(l<N< 50000)头奶牛实在是太难伺候了,她们甚至有自己独特的产奶时段.当 然对于某一头奶牛,她每天的产奶时段是固定的,为时间段A到B包括时间段A和时间段B.显然,约翰必须开发一个调控系统来决定每头奶牛应该被安排到哪个牛棚去挤 奶,因为奶牛们显然不希望在挤奶时被其它奶牛看见.
约翰希望你帮他计算一下:如果要满足奶牛们的要求,并且每天每头奶牛都要被挤过奶,至少需要多少牛棚 •每头牛应该在哪个牛棚被挤奶。如果有多种答案,你只需任意一种即可。
I n p u t Input Input
第1行:单个整数,N
行2…N + 1:行i + 1描述了牛i的挤奶间隔,其中有两个以空格分隔的整数。
O u t p u t Output Output
第1行:谷仓必须具有的最小数量。
第2…N + 1行:第i + 1行描述了第i头牛去的房间
S a m p l e Sample Sample I n p u t Input Input
5
1 10
2 4
3 6
5 8
4 7
S a m p l e Sample Sample O u t p u t Output Output
4
1
2
3
2
4
T r a i n Train Train o f of of T h o u g h t Thought Thought
将牛开始的时间排个序,定义一个小根堆,用来存牛棚
判断是否能进牛棚,能就进,否则开个新的
C o d e Code Code
#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
bool b;
int Corral[50005],Ans[50005],N;
struct cow
{
int start,end,p,answer;//start表示牛开始挤奶的时间,end表示结束的时间,p表示在原数列中的位置,answer表示这头牛去哪个牛棚
}Cow[50005];
bool operator <(const cow &a,const cow &c){return a.end>c.end;}//开堆
bool cmp(cow x,cow y)
{return x.start<y.start;}
bool cmp2(cow x,cow y)
{return x.p<y.p;}
int main()
{
int ans=0;
scanf("%d",&N);
for (int i=1; i<=N; ++i)
scanf("%d%d",&Cow[i].start,&Cow[i].end),Cow[i].p=i;//记录原序(输出)
sort(Cow+1,Cow+N+1,cmp);
priority_queue<cow>Carrol;
Cow[1].answer=1;
Carrol.push(Cow[1]);
ans=1;
for (int i=2; i<=N; ++i)
{
cow t=Carrol.top();
if (t.end>=Cow[i].start)
{
Cow[i].answer=++ans;
Carrol.push(Cow[i]);
}//需要新牛棚
else {
Carrol.pop();
Cow[i].answer=t.answer;
Carrol.push(Cow[i]);
}//可以换掉
}
sort(Cow+1,Cow+N+1,cmp2);//排回原序
printf("%d\n",ans);
for (int i=1; i<=N; ++i)
printf("%d\n",Cow[i].answer);
}