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) 挑剔的奶牛! 他们如此挑剔以致于 ...
随机推荐
- LeetCode OJ:Palindrome Linked List(回文链表判断)
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- react-hooks: CSSProperties
1.定义变量 const divStyle: React.CSSProperties = { width: "11rem", height: "7rem", b ...
- PostgreSQL 日常SQL记录
平时用的比较多的SQL语句,有时候会忘掉一点点,在这里记录一下: 1.创建表的同时,插入数据: create table test as select generate_series(1, 10000 ...
- Win7系统64位环境下使用Apache——下载mod_jk
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/70313329 本文出自[我是干勾鱼的博客] 之前在几篇文章: Win7系统64位 ...
- jQuery attr 与 prop 区别最简单分析
比较经典的解释: 在处理html元素本身就带有的固有属性时,使用prop方法,对于html元素中,我们自己定义的dom属性时,使用attr方法. 而咱自己的理解是: attr会忠实的获取设置dom标签 ...
- ubuntu 添加新硬盘
查看硬盘: # fdisk -l ... Disk /dev/sdb: 274.9 GB, 274877906944 bytes 255 heads, 63 sectors/track, 33418 ...
- 解放内存之搭建自己的 R Server
学校的课五门有四门需要跑R程序,有一些长长长的代码实在是占用了太多的内存,果断决定搭个R的服务器放着自己跑. 愉快的是,R studio server 的搭建真心简单快捷~这个从前被我忽略的东东终于排 ...
- sublime上配置markdown
等等等等 简书一个不错的教程:Sublime Text3的Markdown配置 补充说明:第一步可以直接找 Tools-->install package control. ^.^ ...
- bzoj 2734 集合选数
Written with StackEdit. Description <集合论与图论>这门课程有一道作业题,要求同学们求出\(\{1, 2, 3, 4, 5\}\)的所有满足以 下条件的 ...
- 让maven生成可运行jar包
平时项目大多用到的是war包,今天实现了一个简单功能,无需部署到web服务器上,只需本地跑java代码即可,因此只要生成一个jar包.那么怎么让maven项目打成一个可以使用java命令跑的jar包呢 ...