畜栏预订
题目链接
#include<iostream>
using namespace std;
#include<algorithm>
#include<set>
#include<queue>
const int N=50005;
typedef pair<int,int> PII;
pair<PII,int> cows[N];
int id[N];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>cows[i].first.first>>cows[i].first.second;
cows[i].second=i;
}
sort(cows,cows+n);
priority_queue<PII,vector<PII>,greater<PII> > heap;
for(int i=0;i<n;i++)
{
if(heap.empty() || heap.top().first >= cows[i].first.first)
{
PII stall={cows[i].first.second,heap.size()+1};
id[cows[i].second]=stall.second;
heap.push(stall);
}
else
{
auto stall=heap.top();
heap.pop();
id[cows[i].second]=stall.second;
stall.first=cows[i].first.second;
heap.push(stall);
}
}
cout<<heap.size()<<endl;
for(int i=0;i<n;i++) cout<<id[i]<<endl;
return 0;
}