POJ-3190 Stall Reservations
来源:互联网 发布:股市九宫图软件 编辑:程序博客网 时间:2024/06/04 05:06
题目传送门
Stall Reservations
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7741 Accepted: 2742 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.
Source
USACO 2006 February Silver
题意:
农夫有N头奶牛,每头奶牛都有自己的产奶时间段,时间start到时间end,每头牛产奶时必须单独为它安排一间畜栏,不能多头奶牛(大于等于2)同时共用一间,要求根据每头牛的产奶时间段,安排一个合理的方案使得使用畜栏数最少。
思路:
这个题,一看就知道是贪心,可是怎么贪心呢?我们也知道,一般这样的区间问题,基本上是排序问题,这个题目也是如此,说说我的思路吧,我首先将整个时间区间,按照时间start从小到大排序,将时间start最小的奶牛加入正在产奶的priority_queue中,priority_queue中定义时间end更小的优先级更高,现在已经有奶牛在产奶了,所以我们在排好序的时间区间中选择,和正在产奶的优先级最高的时间区间匹配的时间区间,也就是不相交;匹配,说明可以在同一个畜栏产奶,不匹配,就重新安排一个畜栏产奶。
我简单的理解为:正在产奶的结束时间越早越好,等待产奶的开始时间越早越好,这样进行匹配贪心得到最优解。
代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int maxn=5*1e4+100;int cnt[maxn];struct Node{ int start,end,id; bool operator <(const Node &a)const { return a.end<end; }}node[maxn];bool cmp(Node a,Node b){ return a.start<b.start;}int main(){ int n; while(~scanf("%d",&n)) { for(int i=0;i<n;i++) { scanf("%d%d",&node[i].start,&node[i].end); node[i].id=i; } sort(node,node+n,cmp); priority_queue<Node>que; que.push(node[0]); cnt[node[0].id]=1; int ans=1; for(int i=1;i<n;i++) { if(!que.empty()&&que.top().end<node[i].start) { cnt[node[i].id]=cnt[que.top().id]; que.pop(); } else { cnt[node[i].id]=++ans; } que.push(node[i]); } printf("%d\n",ans); for(int i=0;i<n;i++) { printf("%d\n",cnt[i]); } } return 0;}
- poj 3190 Stall Reservations
- POJ-3190-Stall Reservations
- POJ-3190-Stall Reservations
- POJ 3190 Stall Reservations
- POJ-3190-Stall Reservations
- poj 3190Stall Reservations
- poj 3190 Stall Reservations
- poj 3190 Stall Reservations
- POJ 3190 Stall Reservations
- POJ 3190 Stall Reservations
- poj 3190 Stall Reservations
- POJ 3190 Stall Reservations
- poj 3190 Stall Reservations
- POJ 3190 Stall Reservations
- POJ 3190 Stall Reservations
- poj 3190 Stall Reservations
- poj 3190 Stall Reservations
- POJ 3190 Stall Reservations
- MOOC清华《VC++面向对象与可视化程序设计》第1章:空白窗口的创建(空项目)
- 反编译Unity3d 中的dll文件
- 理解ThreadLocal
- 浅拷贝和深拷贝的区别?
- 线段树 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 HihoCoder 1586
- POJ-3190 Stall Reservations
- Qt实现打开网页
- 阿里云 1G内存云主机mysql 配置参数优化
- Linux实现的ARP缓存老化时间原理解析
- 关于环信easeUI3.+的集成
- cocos2dx基础篇(14)——编辑框之一CCTextFieldTTF
- DM9000C
- 我们不是没有生长,我们只是在扎根
- java中实现多态的机制是什么