POJ:3190-Stall Reservations
传送门: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来记录每个奶牛产奶结束的时间,遇到一个奶牛开始产奶,就看优先队列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的更多相关文章
- poj 3190 Stall Reservations
http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)
Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...
- POJ 3190 Stall Reservations (优先队列)C++
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7646 Accepted: 271 ...
- POJ 3190 Stall Reservations【贪心】
POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...
- POJ -3190 Stall Reservations (贪心+优先队列)
http://poj.org/problem?id=3190 有n头挑剔的奶牛,只会在一个精确时间挤奶,而一头奶牛需要占用一个畜栏,并且不会和其他奶牛分享,每头奶牛都会有一个开始时间和结束时间,问至少 ...
- poj 3190 Stall Reservations 贪心 + 优先队列
题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...
- POJ 3190 Stall Reservations 【贪心 优先队列】
题意:给出n头牛必须单独占用一台机器的时间段,问至少需要多少台机器 先按照每头牛的时间的x来排序,然后用一个优先队列(优先选取最小的)维护已经喂好的牛的最小的结束时间 比如现在优先队列里面有m头牛已经 ...
- POJ--3190 Stall Reservations(贪心排序)
这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...
随机推荐
- Python中的循环体
一.循环 1.while语句: while 条件: 循环体 else: 当上面的条件不成立时才会执行 执行顺序:判断条件是否为真.如果为真,执行循环体,再次判断条件如果为假,执行else下代码块 2. ...
- Angular JS + Express JS入门搭建网站
3月份开始,接到了新的任务,跟UI开发有关,用的是Angular JS,Express JS等技术.于是周末顺便学习下新技术. 组里产品UI架构如下: 其中前端,主要使用Angular JS框架,另外 ...
- Linux文件属性与权限
一.在Linux里面,任何一个文件都具有“User,Group,Others”(用户.用户组.其他人)三种身份 二.用户组最有用的功能之一,就是当你在团队开发资源的时候,且每个账号都可以有多个用户组的 ...
- DIV命名规范
DIV命名规范 企业DIV使用频率高的命名方法 网页内容类 --- 注释的写法: /* Footer */ 内容区/* End Footer */ 摘要: summary 箭头: arrow 商标: ...
- MySQL入门很简单: 9 插入 更新与删除数据
1. 插入数据:INSERT 1)为表的所有字段插入数据 第一种: 不指定具体的字段名 INSERT INTO 表名 VALUES(值1,值2,...,值n): 第二种:INSERT语句中列出所有字段 ...
- 触发transition的几种方式--转
鼠标单击 获取焦点 或元素发生任何改变,怎么说呢,目前的理解是,元素发生了什么改变,使得它跟以前不一样了.比如同样是p元素,先有一个样式.后来这个p被hover了.被focus了.或者通过另外一条途径 ...
- mysql索引和正确使用方式
一.索引类型 B树索引:大部分都是,因此B树的特性限制了索引如何使用:必须看看索引的正确使用限制(含组合索引的限制)http://blog.csdn.net/lovemdx/article/detai ...
- Android(java)学习笔记61:Android中的 Application类用法
1. 简介 如果想在整个应用中使用全局变量,在java中一般是使用静态变量,public类型:而在android中如果使用这样的全局变量就不符合Android的框架架构,但是可以使用一种更优雅的方式就 ...
- 成绩累加排名,poj(2153)
题目链接:http://poj.org/problem?id=2153 解题报告: 注意map中的string,因此要将char[]转换为string型. #include <iostream& ...
- 2017.11.18 C语言的算法分析题目
算法分析 1. 选定实验题目,仔细阅读实验要求,设计好输入输出,按照分治法的思想构思算法,选取合适的存储结构实现应用的操作. 2. 设计的结果应在Visual C++ 实验环境下实现并进行调试.(也可 ...