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.

 
 
题意:N头牛,每头牛都有霸占食槽的习惯,从【cow【i】.l,cow【i】.r】,被霸占的食槽,无法给其他牛使用,就必须多几个平行食槽
问最少几个平行食槽可供牛使用,且输出每个牛所在食槽处。
 
思路:一个食槽,能否放入下一个牛,取决于前一个牛的cow【i-1】.r 是否小于 cow【i】.l 
我们可以想到,如果cow【i-1】.r >= cow【i】.l ,  就需要另外开一行食槽。
cow【i-1】.r < cow【i】.l 那我们就可以把当前的cow【i】放到这一行食槽中。 
但是这样不一定是最优的,你虽让能放进去,但是前面牛的越早结束越好。
 这样的话就可以用一个最小堆维护每列牛最右边的食槽,
 
开始我想到这,以为有多少食槽就要建立多少个堆,让后再去找它符合哪个队(显然这样不行),其实只用一个堆就可以了,
一个堆的话如果右边界最小的你都不满足,那么其他的你肯定不满足
我们还需要事前将牛按照左边界排序,因为对于同一个最小的右边界,我们肯定希望塞入左边界最小的牛
 
 
 #include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std; const int maxn = 5e4+;
int n;
struct Node
{
int id;
int l,r;
}; int mp[maxn];
Node cow[maxn];
bool cmp1(Node a,Node b)
{
return a.l < b.l;
} bool operator<(Node a,Node b)
{
return a.r > b.r;
} priority_queue<Node>que;
int main()
{
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d%d",&cow[i].l,&cow[i].r);
cow[i].id = i;
}
sort(cow+,cow++n,cmp1);
int k = ;
for(int i=; i<=n; i++)
{
if(que.empty())
{
mp[cow[i].id] = k;
}
else if(que.top().r < cow[i].l)
{ mp[cow[i].id] = mp[que.top().id];
que.pop();
}
else
mp[cow[i].id] = ++k;
que.push(cow[i]);
}
printf("%d\n",k);
for(int i=; i<=n; i++)
{
printf("%d\n",mp[i]);
}
}
 
 

Stall Reservations POJ - 3190(贪心)的更多相关文章

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

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

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

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11002   Accepted: 38 ...

  3. Greedy:Stall Reservations(POJ 3190)

    牛挤奶 题目大意:一群牛很挑剔,他们仅在一个时间段内挤奶,而且只能在一个棚里面挤,不能与其他牛共享地方,现在给你一群牛,问你如果要全部牛都挤奶,至少需要多少牛棚? 这一题如果把时间区间去掉,那就变成装 ...

  4. poj 3190 贪心+优先队列优化

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4274   Accepted: 153 ...

  5. POJ 3190 Stall Reservations贪心

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

  6. POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)

    Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...

  7. 【POJ - 3190 】Stall Reservations(贪心+优先队列)

    Stall Reservations 原文是English,这里直接上中文吧 Descriptions: 这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于 ...

  8. POJ 3190 Stall Reservations【贪心】

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

  9. poj 3190 Stall Reservations

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

随机推荐

  1. Confluence 6 安全概述和建议概述

    这个文档是针对 Confluence 的系统管理员希望对 Confluence Web应用程序安全性进行评估而设计的.这个页面将对系统的安全进行大致的描述,同时也会对 Confluence 的安全配置 ...

  2. Confluence 6 使用一个主题到站点

    主题被用来在你的 Confluence 站点中应用表现形式.请查看 Working with Themes 页面来查看如何应用你的整个站点和如何添加更多的主题. 希望在站点中应用主题: 进入  > ...

  3. Confluence 6 数据中心的 SAML 单点登录最佳实践和故障排除

    最佳实践 SAML 授权仅仅在有限的时间进行校验.你需要确定运行你的应用的计算机时间与 IdP 的时间是同步的. 如果你应用中的用户和用户组是通过用户目录进行配置的,你通常希望用户来源目录和你的 Id ...

  4. bat如何创建多级文件夹(在android设备中)

    在android设备中要创建多个或者多级文件夹时,手动去创建费时费力(有点傻),一个bat文件就能很好的实现这个功能. 1.首先创建同级多个文件夹且在该文件夹下生成一个文件 @echo off ech ...

  5. Exception类的学习与继承总结

    日期:2018.11.11 星期日 博客期:023 Exception类的学习与继承总结 说起来我们上课还是说过的!老师提到了报错问题出现主要分Exception和Error两类!第一次遇见这个问题是 ...

  6. 《剑指offer》 大数递增

    本题来自<剑指offer> 大数的存储 题目: 针对以下问题:大数的存储.大数的相加.大数的运算. 思路: 当数据较大时候,long long数据已经存储不了,借助数组的方式进行存储. 假 ...

  7. JS:事件循环机制、调用栈以及任务队列

    点击查看原文 写在前面 js里的事件循环机制十分有趣.从很多面试题也可以看出来,考察简单的setTimeout也就是考察这个机制的. 在之前,我只是简单地认为由于函数执行很快,setTimeout执行 ...

  8. python操作注册表

    #注册表操作 # -*- coding: utf-8 -*- import win32api import win32con #打开注册表:传主键化值,子键值,操作方法(win32con.KEY_AL ...

  9. MySQL修改数据表存储引擎的3种方法介绍

    这篇文章主要介绍了MySQL修改数据表存储引擎的3种方法介绍,分别是直接修改.导出导入.创建插入3种方法, 可以参考下   MySQL作为最常用的数据库,经常遇到各种各样的问题.今天要说的就是表存储引 ...

  10. 论文阅读笔记二十八:You Only Look Once: Unified,Real-Time Object Detection(YOLO v1 CVPR2015)

    论文源址:https://arxiv.org/abs/1506.02640 tensorflow代码:https://github.com/nilboy/tensorflow-yolo 摘要 该文提出 ...