大致题意:有一些奶牛只能在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作,给你每头奶牛的指定时间的区间,问你最小需要多少机器,并且输出,每头奶牛具体在由哪个机器来挤奶。
做法:先将这些区间按开始时间从小到大排序, 然后用优先队列判断当前所有区间中结束时间最早的和现在这个区间开始时间的大小,如果当前区间的开始时间比较大,那这头奶牛就是由取出的机器来执行,不然就要加一台机器
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
struct s{
int st, ed, idx, bel;
}pp[50100];
struct num{
int no, val;
num(){}
num(int xx, int yy):no(xx), val(yy){}
bool operator < (const num &a) const{
return val > a.val;
}
};
bool cmp(s a, s b){
return a.st < b.st;
}
bool cmp2(s a, s b){
return a.idx < b.idx;
}
priority_queue<num>que;
int n;
int ans[50100];
int main(){
int i;
while(scanf("%d" ,&n) != EOF){
while(!que.empty()){
que.pop();
}
int tot = 0;
for(i = 1; i <= n; i++){
scanf("%d%d", &pp[i].st, &pp[i].ed);
pp[i].idx = i;
}
sort(pp+1, pp+1+n, cmp);
int res = 1;
que.push(num(1, pp[1].ed));
pp[1].bel = 1;
tot = 1;
for(i = 2; i <= n; i++){
num temp = que.top();
if(pp[i].st > temp.val){
que.pop();
que.push(num(temp.no, pp[i].ed));
pp[i].bel = temp.no;
}
else{
res++;
que.push(num(++tot, pp[i].ed));
pp[i].bel = tot;
}
}
printf("%d\n", res);
sort(pp+1, pp+1+n, cmp2);
for(i = 1; i <= n; i++)
printf("%d\n", pp[i].bel);
}
return 0;
}