传送门:
思路: 使用优先队列将当前奶牛开始工作时间与队列首位结束时间比较,如果大于便使用队列首位奶牛的位置并改变队列,否则增加位置改变队列。
注意:输出的位置是对应输入的,因此在排序时需要使用一个pos记录位置。
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
struct Time
{
int l;
int r;
int pos;
};
Time t[50005];
Time now;
bool operator < (Time a, Time b)
{
if(a.r == b.r)
return a.l > b.l;
return a.r > b.r;
}
bool cmp(Time a, Time b)
{
if(a.l == b.l)
return a.r < b.r;
return a.l < b.l;
}
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &t[i].l, &t[i].r);
t[i].pos = i;
}
sort(t+1,t+n+1,cmp);
priority_queue <Time> q;
q.push(t[1]);
int k = 1;
int s[50005];
memset(s,0,sizeof s);
s[t[1].pos] = 1;
for(int i = 2; i <= n; i++)
{
now = q.top();
if(t[i].l > now.r)
{
q.pop();
q.push(t[i]);
s[t[i].pos] = s[now.pos];
}
else
{
q.push(t[i]);
k++;
s[t[i].pos] = k;
}
}
printf("%d\n",k);
for(int i = 1; i <= n; i++)
printf("%d\n", s[i]);}
}
}