题
这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于必须在[A,B ]的时间内产奶(1 <= A <= B <= 1,000,000)当然, FJ必须为他们创造一个决定挤奶时间的系统.当然,没有牛想与其他奶牛分享这一时光
帮助FJ做以下事:
使每只牛都有专属时间的最小牛棚数
每只牛在哪个牛棚
也许有很多可行解。输出一种即可,采用SPJ
Input
第一行一个数字 N
第 2…N+1行: 第 i+1行 描述了i号奶牛挤奶的起止时间
Output
第一行:牛棚最小数量
Lines 2…N+1: 第 i+1行 描述了i奶牛被安排的牛棚
Sample Input
5
1 10
2 4
3 6
5 8
4 7
Sample Output
4
1
2
3
2
4
代码
#include "stdio.h"
#include "algorithm"
#include "iostream"
#include "queue"
using namespace std;
struct ppp
{
int a,b,k;
bool operator<(const ppp &q)const
{
return b>q.b;
//优先队列排序相反(与sort不同,在sort中>为从大到小,而在优先队列中是从小到大,<同理)
}
} p[50009],t;
bool paixu(ppp x,ppp y)
{
if(x.a!=y.a)
return x.a<y.a;
return x.b<y.b;
}
int c[50009];
priority_queue<ppp>q;
int main()
{
int n,l=1;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
cin>>p[i].a>>p[i].b;
p[i].k=i;
}
sort(p+1,p+n+1,paixu);
c[p[1].k]=1;//运用的很巧妙
q.push(p[1]);
for(int i=2; i<=n; i++)
{
t=q.top();
if(p[i].a>t.b)
{
q.pop();
q.push(p[i]);
c[p[i].k]=c[t.k];
}
else
{
q.push(p[i]);
c[p[i].k]=++l;
}
}
cout<<l<<endl;
for(int i=1; i<=n; i++)
cout<<c[i]<<endl;
}