标签:
Description
Input
Output
Sample Input
5 1 10 2 4 3 6 5 8 4 7
Sample Output
4 1 2 3 2 4
Hint
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.
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
struct node{
int x,y,i;
bool operator <(const node &a)const {
if(y==a.y)return x>a.x;
return y>a.y; //先结束的排前面
}
}e[50005];
bool cmp(node a,node b){
if(a.x==b.y)return a.y<b.y;
return a.x<b.x;
}
int use[50005];
priority_queue<node>sp;
int main(){
int n,i,j;
while(scanf("%d",&n)!=EOF){
for(i=1;i<=n;i++){
scanf("%d%d",&e[i].x,&e[i].y);
e[i].i=i;
}
sort(e+1,e+1+n,cmp);
int cnt=1;
use[e[1].i]=1;
sp.push(e[1]);
for(i=2;i<=n;i++){
if(!sp.empty()&&sp.top().y<e[i].x){//如果队列非空且最先结束的机器的时间小于当前奶牛开始时间
use[e[i].i]=use[sp.top().i];
sp.pop();
}
else{//否则就再加一台机器
cnt++;
use[e[i].i]=cnt;
}
sp.push(e[i]);
}
printf("%d\n",cnt);
for(i=1;i<=n;i++){
printf("%d\n",use[i]);
}
while(!sp.empty())sp.pop();
}
return 0;
}标签:
原文地址:http://blog.csdn.net/black_miracle/article/details/51359956