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) 挑剔的奶牛! 他们如此挑剔以致于 ...
随机推荐
- NSURLConnection / NSURLSession/ SDWebImage
1. NSURLConnection (iOS9开始被弃用)=========================================== 此类的对象加载一个URL请求对象,通过异步/同步的方 ...
- javascript 继承的两种方式
js中继承可以分为两种:对象冒充和原型链方式 一.对象冒充包括三种:临时属性方式.call()及apply()方式1.临时属性方式 代码如下: function Person(name){ t ...
- 几个Unity3d UI制作的解决方案.
1.ex2D的渲染机制 (高效的原因) 在以往的2D插件中,渲染方式是每个sprite单独渲染,由Unity负责Dynamic Batching.在新版ex2D中,我们经过严谨的优化实现了独立的dyn ...
- python 环境变量设置
Win+E --> 我的电脑 --> 右击 选择属性 --> 高级系统设置 --> 环境变量 --> 用户变量 中 找到 PATH(Path) --> 编辑 --& ...
- (效果二)js实现两个变量值的交换
ES5: var a = 12,b=13,c; c = a; a = b; b = c; console.log(a,b);//13,12 通过设置第三方变量交换赋值来实现 ES6 var a = ...
- 记录最近工作使用javascript对select[option]的操作
1: 数据库取值赋予select选项 $(function(){ $("input[name='state'][value='{$store.state}']").attr(&qu ...
- Supervisor进程监控
安装 yum install -y python-setuptools easy_install supervisor echo_supervisord_conf > /etc/supervis ...
- Git克隆、修改、更新项目,及查看项目地址命令
第一步:在本地新建一个文件夹,作为本地仓库,如“texzt”,直接打开该文件夹,并单击右键,选择git bash here 则可以直接进入到该文件夹目录下. 第二步:将本地仓库初始化,命令:git i ...
- Ubuntu15.10下Hadoop2.6.0伪分布式环境安装配置及Hadoop Streaming的体验
Ubuntu用的是Ubuntu15.10Beta2版本,正式的版本好像要到这个月的22号才发布.参考的资料主要是http://www.powerxing.com/install-hadoop-clus ...
- 在PHP中对查询出得数据库数据进行json编码
select.php <?php $con = mysql_connect("localhost","Thh","920920thh" ...