Stall Reservations(贪心+优先队列)
Description
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
Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.
Output
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
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.
思路:
首先根据挤奶时间的先后顺序排序。。。然后将第一头牛加入优先队列。。然后就是加入优先队列的牛应该根据越早结束挤奶那么优先级更高,如果时间结束点相等,那么开始时间早的优先级高。。。
然后从前向后枚举。如果碰到有牛的挤奶时间的开始值大于优先队列的首部的结束值,那么说明这两头牛可以一起公用一个挤奶房。。然后从优先队列中删除这头牛。。那么这个问题就得到解决了。。。这道题用优先队列主要是优化时间复杂度;
#include<string.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
int start_time;
int end_time;
int num;
friend bool operator<(node a,node b)
{
if( a.end_time==b.end_time)
return a.start_time>b.start_time;
return a.end_time>b.end_time;
} }arr[],now;
bool cmp(node a,node b)
{
if(a.start_time==b.start_time)
return a.end_time<b.end_time;
return a.start_time<b.start_time;
}
int ans[]={};
int main()
{
int i,n,l=,MAX=-;
while(scanf("%d",&n)!=-)
{
l=;
priority_queue<node>s,q;
for(i=;i<n;i++)
{
scanf("%d%d",&arr[i].start_time,&arr[i].end_time);
arr[i].num=i;
}
sort(arr,arr+n,cmp);
s.push(arr[]);
ans[s.top().num]=l;
for(i=;i<n;i++)
{
now=s.top();
if(arr[i].start_time>now.end_time)
{
ans[arr[i].num]=ans[now.num];
s.pop();
}
else
ans[arr[i].num]=++l;
s.push(arr[i]);
}
printf("%d\n",l);
for(i=;i<n;i++)
{
printf("%d\n",ans[i]); }
}
return ;
}
Stall Reservations(贪心+优先队列)的更多相关文章
- poj 3190 Stall Reservations 贪心 + 优先队列
题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...
- poj3190 Stall Reservations (贪心+优先队列)
Cleaning Shifts Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- [USACO06FEB] Stall Reservations 贪心
[USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...
- 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 ...
- [POJ3197]Stall Reservations (贪心)
题意 (来自洛谷) 约翰的N(l<N< 50000)头奶牛实在是太难伺候了,她们甚至有自己独特的产奶时段.当 然对于某一头奶牛,她每天的产奶时段是固定的,为时间段A到B包括时间段A和时间段 ...
- POJ3190 Stall Reservations 贪心
这是个典型的线程服务区间模型.一些程序要在一段时间区间上使用一段线程运行,问至少要使用多少线程来为这些程序服务? 把所有程序以左端点为第一关键字,右端点为第二关键字从小到大排序.从左向右扫描.处理当前 ...
- Stall Reservations POJ - 3190 (贪心+优先队列)
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11002 Accepted: 38 ...
- 【POJ - 3190 】Stall Reservations(贪心+优先队列)
Stall Reservations 原文是English,这里直接上中文吧 Descriptions: 这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于 ...
随机推荐
- ZOJ 3204 Connect them(最小生成树+最小字典序)
Connect them Time Limit: 1 Second Memory Limit: 32768 KB You have n computers numbered from 1 t ...
- Node net模块与http模块一些研究
这周遇到一个有意思的需求,端上同学希望通过 socket 传送表单数据(包含文件内容)到 node 端,根据表单里的文件名.手机号等信息将文件数据保存下来.于是我这样写了一下--socket_serv ...
- python基础之多线程与多进程(一)
并发编程? 1.为什么要有操作系统? 操作系统,位于底层硬件与应用软件之间 工作方式:向下管理硬件,向上提供接口 2.多道技术? 不断切换程序. 操作系统进程切换: 1.出现IO操作 2.固定时间 进 ...
- NAVagationController
UINavigationController为导航控制器,在iOS里经常用到. 1.UINavigationController的结构组成 UINavigationController有Navigat ...
- 【CSAPP】一、计算机系统漫游
一.位+上下文 文本文件 / 二进制文件: 文本文件是只由ASCII码构成的文件 二.从源代码到可执行文件的顺序 源代码 ——> 可执行文件(机器代码)共有四步: 全过程代码 gcc hello ...
- 6.etc目录下重要文件和目录详解
1./etc/下的重要的配置文件 /etc(二进制软件包的 yum /rpm 安装的软件和所有系统管理所需要的配置文件和子目录.还有安装的服务的启动命令也放置在此处) /etc/sysconfig/n ...
- markdown 语法汇总
Markdown是什么? Markdown是一种轻量级标记语言,它以纯文本形式(_易读.易写.易更改_)编写文档,并最终以HTML格式发布. Markdown也可以理解为将以MARKDOWN语言编写的 ...
- Python 调用C函数
/******************************************************************** * Python 调用C函数 * 说明: * Tony在处理 ...
- Python标准库之time和datetime
注:博客转载自:https://www.cnblogs.com/zhangxinqi/p/7687862.html 1.python3日期和时间 Python 程序能用很多方式处理日期和时间,转换日期 ...
- django中的@login_required
转:http://www.cnblogs.com/ccorz/p/Django-zhong-loginrequired-yong-fa-jian-jie.html 1.网站开发时的登录需求: ===用 ...