传送门: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. svg用作背景图

    svg用做背景图的几种方式 1. 直接使用 background: url('data:image/svg+xml;charset=utf-8,<svg width="10" ...

  2. polyfill 一个解决兼容的绝佳方案

    polyfill为何物 Polyfill你可以理解为“腻子”,就是装修的时候,可以把缺损的地方填充抹平. 举个例子,html5的storage(session,local), 不同浏览器,不同版本,有 ...

  3. 关于SAP UI5数据绑定我的一些原创内容

    如何查找SAP UI5官方关于数据绑定的文档: https://sapui5.hana.ondemand.com/ 点Documentation: Filter里输入data就能看到Data Bind ...

  4. IOS SQLite函数总结

    SQL语句的种类 ●  数据定义语句(DDL:Data Definition Language) ●  包括create和drop等操作 ●  在数据库中创建新表或删除表(create table或 ...

  5. 使用extentreports美化testng报告2,增加监听

    有兴趣研究了extentreports报告美化插件,但是因为发现插件有很多内容不能自定义,所以放弃了这个插件,我扩充了官方代码的demo,在testng中增加了监听,并打印了一些测试用例,现在我把两个 ...

  6. POJ-3669 Meteor Shower---BFS+预处理

    题目链接: https://vjudge.net/problem/POJ-3669 题目大意: 巨大流星雨即将袭来.每个流星会对击中的地方以及周围(上下左右四格)造成破坏.Bessie开始时位于(0, ...

  7. bzoj4836 [Lydsy2017年4月月赛]二元运算

    Description 定义二元运算 opt 满足 现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c  你需要求出有多少对 (i, j) ...

  8. assert函数和捕获异常

    assert函数: C语言和C++都有一个专为调试而准备的工具函数,就是 assert()函数. 这个函数是在C语言的 assert.h 库文件里定义的,所以包含到C++程序里我们用以下语句: #in ...

  9. Linux 启动、停止、重启tomcat工具(Shell脚本)

    1.   启动 #!/bin/bash pids=`ps -ef | grep java | grep -w tomcat | awk '{print $2}'` #pids=`ps -ef | gr ...

  10. python—命名空间、作用域查找顺序、闭包

    名称空间 name space,如下图: x = 1, 1存放在内存中,1 会有一个内存地址,x 则 存放在 name space 里,并同时记录了 1的内存地址, 即 名称空间是存放了变量x与1绑定 ...