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.

 
题解:整理区间,将不重合的区间整理成一个区间
需要用到优先队列
当优先队列的元素是结构体时候,需要在结构体内重载比较操作符函数。
 struct node{
int a;
int b;
int flag;
friend bool operator <(node node1,node node2)
{
//<为从大到小排列,>为从小到大排列
return node1.b<node2.b;
}
}n[];
 #include<iostream>
#include<algorithm>
#include<queue>
using namespace std; struct node{
int a; //开始时间
int b; //结束时间
int c; //序列号
int flag; //区间安排序号
friend bool operator <(node node1,node node2) //重载比较操作符函数
{
return node1.b > node2.b;
}
}cows[]; bool cmp1(node t1,node t2) //根据区间排序
{
if(t1.a != t2.a) return t1.a < t2.a;
else return t1.b < t2.b;
} bool cmp2(node t1,node t2) //根据序列号排序
{
return t1.c < t2.c;
} int main()
{
int n;
priority_queue<node>que;
while(~scanf("%d",&n))
{
for(int i = ; i < n; i++)
{
scanf("%d %d",&cows[i].a,&cows[i].b);
cows[i].c = i;
}
sort(cows,cows+n,cmp1);
int ans = ;
cows[].flag = ans;
que.push(cows[]); for(int i = ; i < n; i++)
{
if(que.top().b >= cows[i].a) //此时cows的区间与que内的任意区间有重合
{
ans++;
cows[i].flag = ans;
}
else
{
cows[i].flag = que.top().flag;
que.pop();
}
que.push(cows[i]);
} sort(cows,cows+n,cmp2);
cout<<ans<<endl;
for(int i = ; i < n; i++)
{
printf("%d\n",cows[i].flag);
}
}
return ;
}

Stall Reservations的更多相关文章

  1. poj 3190 Stall Reservations

    http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  2. BZOJ1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 509  Sol ...

  3. Stall Reservations(POJ 3190 贪心+优先队列)

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4434   Accepted: 158 ...

  4. BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )

    线段树.. -------------------------------------------------------------------------------------- #includ ...

  5. BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 553   ...

  6. POJ3190 Stall Reservations 【贪婪】

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3106   Accepted: 111 ...

  7. 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 566  Sol ...

  8. POJ 3190 Stall Reservations贪心

    POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...

  9. POJ 3190 Stall Reservations (优先队列)C++

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7646   Accepted: 271 ...

  10. POJ--3190 Stall Reservations(贪心排序)

    这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...

随机推荐

  1. python/*args和**kwargs

    *args和**kwargs #coding=utf8 __author__ = 'Administrator' # 当函数的参数不确定时,可以使用*args和**kwargs.*args没有key值 ...

  2. python JavaScript

    JavaScript 一. JavaScript Javascript 在开发中绝大多数情况是基于对象的.也是面向对象的. a. JavaScript的引入方式 1 2 3 4 5 6 7 #直接编写 ...

  3. Linux安装git和maven的详细过程

    一.使用yum安装git 当前安装环境是centos6.5 由于在CentOS6的yum源中已经有git的版本了,可以直接使用yum源进行安装 yum -y install git 由于centos6 ...

  4. SpringMVC(一):搭建一个SpringMVC helloword项目

    操作步骤: 1)下载spring framework开发包,给eclipse安装spring开发插件,如何安装开发插件&下载开发包请参考我的博文:<Spring(一):eclipse上安 ...

  5. MySQL 如何使用 PV 和 PVC?- 每天5分钟玩转 Docker 容器技术(154)

    本节演示如何为 MySQL 数据库提供持久化存储,步骤为: 创建 PV 和 PVC. 部署 MySQL. 向 MySQL 添加数据. 模拟节点宕机故障,Kubernetes 将 MySQL 自动迁移到 ...

  6. 原生js的一些研究和总结(1)

    数据类型 基本类型值包括: undefined,null,Boolean,Number和String,这些类型分别在内存中占有固定的大小空间,它们的值保存在栈空间,我们通过按值来访问的. 引用类型包括 ...

  7. eclipse下如何使用Hibernate反转工程生与数据库对应的实体类和映射文件(以MySQL为例)

    首先需要为eclipse添加对Hibernate的支持(也就是下载的Hibernate中的jar包),下载方法另查,这里不多做阐述. 想要使用反转工程,首先要下载Hibernate反转工程的插件Jbo ...

  8. 去除Eclipse中js报错的问题

    第一步:    去除eclipse的JS验证:        将windows->preference->Java Script->Validator->Errors/Warn ...

  9. JavaScript数据结构与算法(八) 集合(ECMAScript 6中定义的类似的Set类)

    TypeScript方式实现源码 // 特性: // 1. 集合是由一组无序且唯一(即不能重复)的项组成的.这个数据结构使用了与有限集合相同的数学概念,但应用在计算机科学的数据结构中. // 2. 也 ...

  10. 1086: [SCOI2005]王室联邦

    1086: [SCOI2005]王室联邦 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 1554  Solved: ...