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>

poj 3190 贪心+优先队列优化的更多相关文章

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

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

  2. POJ 2431 贪心+优先队列

    题意:一辆卡车距离重点L,现有油量P,卡车每前行1米耗费油量1,途中有一些加油站,问最少在几个加油站加油可使卡车到达终点或到达不了终点.   思路:运用优先队列,将能走到的加油站的油量加入优先队列中, ...

  3. POJ 3190 Stall Reservations贪心

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

  4. POJ 3190 Stall Reservations【贪心】

    POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...

  5. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

  6. poj 1511 优先队列优化dijkstra *

    题意:两遍最短路 链接:点我 注意结果用long long #include<cstdio> #include<iostream> #include<algorithm& ...

  7. Dijkstra算法(朴素实现、优先队列优化)

    Dijkstra算法只能求取边的权重为非负的图的最短路径,而Bellman-Ford算法可以求取边的权重为负的图的最短路径(但Bellman-Ford算法在图中存在负环的情况下,最短路径是不存在的(负 ...

  8. poj 1862 Stripies/优先队列

    原题链接:http://poj.org/problem?id=1862 简单题,贪心+优先队列主要练习一下stl大根堆 写了几种实现方式写成类的形式还是要慢一些... 手打的heap: 1: #inc ...

  9. 【BZOJ 1150】 1150: [CTSC2007]数据备份Backup (贪心+优先队列+双向链表)

    1150: [CTSC2007]数据备份Backup Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味 的,因此你想设 ...

随机推荐

  1. dmesg、stat命令

    一.dmesg:系统启动异场诊断. 语法:       dmesg [选项] 参数: -C,-清除               清除环形缓冲区. -c,--read-clear             ...

  2. QObject::connect可以对不继承QObject的类也使用信号槽

    #include <QCoreApplication>#include <QDebug>#include <QObject>#include <QThread ...

  3. Java String类和StringBuffer类

    1.String类 用于字符串操作,本质是字符数组char[ ]. String对象的值是不可变的,即当你修改一个String对象的类容,JVM不会改变原来的对象,而是生成一个新String对象. 2 ...

  4. 日历控件datetimepicker(IE11)

    1.安装 smalot.bootstrap-datetimepicker 2.引用 bootstrap.css bootstrap-datetimepicker.min.css jquery-1.10 ...

  5. [转载]GridView中点击某行的任意位置就选中该行

    原文链接:http://www.cnblogs.com/Echo529/p/4521701.html GridView中点击某行的任意位置就选中该行 分类: 第一步:添加选择列 点击GridView右 ...

  6. linq to sql之Distinct

    Distict用来排除相同序列中元素的,对于基础类型,可以直接使用Distinct,如:int[] a = {1, 2, 2, 3, 3, 3, 4};var reslut = a.Distinct( ...

  7. BZOJ4887可乐题解--矩阵运算

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=4887 分析 话说这道题经常见到类似模型来计数算期望,概率啊,然而我太蒻了都不会做,今天看 ...

  8. pymssql文档(转)

    pymssql methods set_max_connections(number) -- Sets maximum number of simultaneous database connecti ...

  9. O055、Detach Volume 操作

    参考https://www.cnblogs.com/CloudMan6/p/5636510.html     本节我们开始学习 Volume Detach 操作,就是将Volume从Instance上 ...

  10. Windows defender怎么才能彻底关闭?

    据不久前的一项测试表明,Windows系统自带的Windows defender软件在所有参与测试的杀毒安全软件中对win10的运行速度影响最大. 而Win10系统的Windows defender会 ...