Stall Reservations
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4274   Accepted: 1530   Special Judge

Description

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which
stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows. 



Help FJ by determining:

  • The minimum number of stalls required in the barn so that each cow can have her private milking period
  • An assignment of cows to these stalls over time

Many answers are correct for each test dataset; a program will grade your answer.

Input

Line 1: A single integer, N 



Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.

Output

Line 1: The minimum number of stalls the barn must have. 



Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

Sample Input

5
1 10
2 4
3 6
5 8
4 7

Sample Output

4
1
2
3
2
4

Hint

Explanation of the sample: 



Here's a graphical schedule for this output:

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.

<span style="font-size:18px;color:#3366ff;">#include <iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
struct Node{
int s,e,id,num;
bool operator<(const Node &a) const
{
return a.e<e;
}
}node[50005];
int cmp(Node a,Node b)
{
if(a.s!=a.s)
return a.e<b.e;
else return a.s<b.s;
}
int cmp2(Node a,Node b)
{
return a.id<b.id;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d %d",&node[i].s,&node[i].e);
node[i].id=i;
}
sort(node+1,node+n+1,cmp);
priority_queue<Node> q;
node[1].num=1;
q.push(node[1]);
int cnt=1;
for(int i=2;i<=n;i++)
{
Node temp=q.top();
if(node[i].s<=temp.e)
{
cnt++;
node[i].num=cnt;
q.push(node[i]);
}
else
{
node[i].num=temp.num;
q.pop();
q.push(node[i]);
}
}
sort(node+1,node+n+1,cmp2);
printf("%d\n",cnt);
for(int i=1;i<=n;i++)
printf("%d\n",node[i].num);
}
return 0;
}</span>

最新文章

  1. sh1.shell脚本练习
  2. 二分图&amp;网络流&amp;最小割等问题的总结
  3. 如何合理优化WEB前端 高效提升WEB前端性能
  4. TIJ——Chapter Eight:Polymorphism
  5. 实现VS2010整合NUnit进行单元测试(转载)
  6. 温故知新---重读C#InDepth(二)
  7. JeeWx 微信管家平台
  8. uc_key getshell
  9. C++模板实例化(1)
  10. 【转】深入解析cookie
  11. 如何解决因为找不到Notepad++的安装路径而导致的不能更新CS-Script的问题
  12. bzoj 3238 Ahoi2013 差异
  13. VMware虚拟机三种网络模式的区别(上篇)
  14. python2到python3的转换以及f.write在python3 中的用法
  15. MM们,你们为什么要找一个程序猿男票?
  16. 从零开始系列之vue全家桶(3)安装使用vuex
  17. 记录学习antd design pro dva的过程,主要记错, 多图预警,如有理解偏差,忘指出,多谢!
  18. SQL 注入检查
  19. js方法的积累
  20. 配置cron定时任务

热门文章

  1. 反射获取config实体类属性并赋值
  2. O061、Boot from Volume
  3. Laravel 实现指定用户下的设备分页(与查询指定分类下的文章原理相同)
  4. hive用户自定义函数
  5. input 禁止输入特殊字符
  6. XSS防御和绕过2
  7. 描述Cookie和Session的作用,区别和各自的应用范围,Session工作原理
  8. Docker Ubuntu容器安装ping
  9. 第十章、numpy模块
  10. deep_learning_凹凸函数