链接:点击打开链接
题意:有N堂课,每堂课有一个开始时间和结束时间,每个教室只能上一堂课,问最少需要几个教室,并输出每堂课在哪个教室上,答案不唯一
代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <climits>
#include <queue>
#include <algorithm>
using namespace std;
struct node{
int no,num,sign;
};
bool operator<(node a,node b){
if(a.num==b.num)
return a.sign<b.sign;
return a.num>b.num;
}
int main(){
node temp;
int n,i,st,en,ans,sum,flag,sum_max;
int G[50005];
while(scanf("%d",&n)!=EOF){
priority_queue<node>q;
queue<int> q_out;
for(i=0;i<n;i++){
scanf("%d%d",&st,&en);
temp.num=st,temp.sign=1,temp.no=i;
q.push(temp);
temp.num=en,temp.sign=-1,temp.no=i;
q.push(temp);
} //num代表时间,sign代表是开始时间还是结束时间,no代表编号
flag=0;sum=1;sum_max=INT_MIN;
G[q.top().no]=sum;
q.pop();
ans=1;
while(!q.empty()){
sum+=q.top().sign;
if(flag==0&&q.top().sign==1)
G[q.top().no]=sum; //没空房间则按顺序放
else if(flag!=0&&q.top().sign==1){
G[q.top().no]=G[q_out.front()]; //有空房间则按空房间的先后顺序放
q_out.pop();
flag--;
}
if(q.top().sign==-1){
q_out.push(q.top().no);
flag++; //如果有空房间则计数,并将编号压入队列
}
q.pop();
if(sum>sum_max)
sum_max=sum;
}
printf("%d\n",sum_max);
for(i=0;i<n;i++)
printf("%d\n",G[i]);
}
return 0;
}