//奶牛挤奶时间区间给出,每个奶牛只能单独挤,最少多少个棚子,输出最少数每一个牛所待棚子
//错误:循环时应从1开始,写作零。以后要思考周全呀!
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
//#include<bits/stdc++.h>//这个头文件以后不用啦,老出错唉
using namespace std;
const int maxn=50050;
int flag[maxn];//大的数组尽量定在外面
struct node{
int l,r,pos;
bool operator < (const node &a)const
{
if(r==a.r) return l>a.l;
return r>a.r;
}
}a[maxn];
int cmp(node c,node b)
{
if(c.l==b.l)
return c.r<b.r;
return c.l<b.l;
}
priority_queue<node>s;
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i].l>>a[i].r;
a[i].pos=i;
}
sort(a,a+n,cmp);
s.push(a[0]);
int kj=1;
flag[a[0].pos]=1;
for(int i=1;i<n;i++)
{
if(s.empty()) break;
if(s.top().r<a[i].l)
{
flag[a[i].pos]=flag[s.top().pos];
s.pop();
}
else {kj++;flag[a[i].pos]=kj;}
s.push(a[i]);
}
cout<<kj<<endl;
for(int i=0;i<n;i++)
{
cout<<flag[i]<<endl;
}
/* while(!s.empty())
{
cout<<s.top().l<<" "<<s.top().r<<endl;
s.pop();
}
cout<<endl;
for(int i=0;i<n;i++)
{
cout<<a[i].l<<" "<<a[i].r<<endl;
}
*/
}
【简单贪心+优先对列】C - Stall Reservations POJ - 3190
最新推荐文章于 2020-05-11 22:18:54 发布