传送门:http://poj.org/problem?id=3190

Stall Reservations

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 9173 Accepted: 3208 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.


解题心得:

  1. 题意就是每个牛在一段时间内要产奶,每个奶牛在产奶的时候必须给它安排一间屋子,问最少需要多少间屋子,以及给每个奶牛安排产奶的屋子是第几间。
  2. 也就是一个思维题 ,先把奶牛用起始时间拍个序,用一个优先队列1来放所有的屋子编号,再用一个优先队列2来记录每个奶牛产奶结束的时间,遇到一个奶牛开始产奶,就看优先队列2中在当前时间是否有奶牛已经结束产奶了,如果已经结束产奶,就将安排给那个产奶的奶牛屋子还回队列1当中,然后从队列1中找一个编号最小的屋子给当前的奶牛。优先队列1中出现的房间号数字最大的就是最少需要安排的房间数。

#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 5e4+100;
struct COW {
int s,e,num;
bool operator <(const COW & a) const {
return a.e < e;
}
}cow[maxn];
int n; bool cmp(COW a,COW b) {
if(a.s == b.s)
return a.e < b.e;
return a.s < b.s;
} void init() {
for(int i=1;i<=n;i++) {
scanf("%d%d",&cow[i].s,&cow[i].e);
cow[i].num = i;
}
sort(cow+1,cow+n+1,cmp);
} int ans[maxn];
void get_ans() {
int Max_num = -1;
priority_queue <int,vector<int>,greater <int> > qu;
priority_queue <COW> qu2;
for(int i=1;i<=maxn;i++)
qu.push(i);
for(int i=1;i<=n;i++) {
COW now = cow[i];
while(!qu2.empty() && qu2.top().e < now.s) {
qu.push(ans[qu2.top().num]);
qu2.pop();
}
ans[now.num] = qu.top();
if(qu.top() > Max_num)
Max_num = qu.top();
qu.pop();
qu2.push(cow[i]);
}
printf("%d\n",Max_num);
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);
} int main() {
scanf("%d",&n);
init();
get_ans();
return 0;
}

POJ:3190-Stall Reservations的更多相关文章

  1. poj 3190 Stall Reservations

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

  2. POJ 3190 Stall Reservations贪心

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

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

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

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

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

  5. POJ 3190 Stall Reservations【贪心】

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

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

    http://poj.org/problem?id=3190 有n头挑剔的奶牛,只会在一个精确时间挤奶,而一头奶牛需要占用一个畜栏,并且不会和其他奶牛分享,每头奶牛都会有一个开始时间和结束时间,问至少 ...

  7. poj 3190 Stall Reservations 贪心 + 优先队列

    题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...

  8. POJ 3190 Stall Reservations 【贪心 优先队列】

    题意:给出n头牛必须单独占用一台机器的时间段,问至少需要多少台机器 先按照每头牛的时间的x来排序,然后用一个优先队列(优先选取最小的)维护已经喂好的牛的最小的结束时间 比如现在优先队列里面有m头牛已经 ...

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

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

随机推荐

  1. (12)JavaScript之[事件][事件监听]

    事件 /** * 事件: * onload 和 onunload 事件在用户进入或离开页面时被触发 * * onchange事件常结合对输入字段的验证来使用 * onmouseover 和 onmou ...

  2. window系统安装jdk,jre

    java开发少不了安装jdk.当然如果只是想运行其他人的java项目,只需要安装jre就行了,不需要安装jdk,jdk是编译用的.jdk可以同时安装多个 版本,只需要在项目部署时注意切换版本选择.在这 ...

  3. Hive建模

    Hive建模 1.介绍 Hive作为数据仓库,同关系型数据库开发过程类似,都需要先进行建模,所谓建模,就是对表之间指定关系方式.建模在hive中大致分为星型.雪花型和星座型.要对建模深入理解,首先需要 ...

  4. Shiro Demo:SpringBoot+Shiro+Druid+MyBatis

    访问start.spring.io生成项目: 然后选择依赖: pom.xml: <?xml version="1.0" encoding="UTF-8"? ...

  5. 2017.11.18 C语言的算法分析题目

    算法分析 1. 选定实验题目,仔细阅读实验要求,设计好输入输出,按照分治法的思想构思算法,选取合适的存储结构实现应用的操作. 2. 设计的结果应在Visual C++ 实验环境下实现并进行调试.(也可 ...

  6. HTTP 之缓存

    这是一篇知识性的文档,主要目的是为了让Web缓存相关概念更容易被开发者理解并应用于实际的应用环境中.为了简要起见,某些实现方面的细节被简化或省略了.如果你更关心细节实现则完全不必耐心看完本文,后面参考 ...

  7. 第51章 设置FLASH的读写保护及解除—零死角玩转STM32-F429系列

    第51章     设置FLASH的读写保护及解除 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.co ...

  8. MySQL事务隔离级别 解决并发问题

    MySQL事务隔离级别 1. 脏读: 骗钱的手段, 两个窗口或线程分别调用数据库转账表,转账后未提交,对方查看到账后,rollback,实际钱没转. 演示方法: mysql默认的事务隔离级别为repe ...

  9. ReactiveObjC框架的简单介绍

    最近在一直在学习RAC框架的Object-C版本ReactiveObjC(Swift版本为ReactiveSwift),这篇文章简单展示一下学习的成果!!!如果有什么地方理解错误,欢迎大家指正!!!互 ...

  10. JS - 简单的下载图片至本地

    <iframe id="saveImg" src="图片路径" style="display:none;"></ifram ...