poj 3190 Stall Reservations (贪心+优先队列)
2015-08-27 20:38
399 查看
Description
Input
Output
Sample Input
Sample Output
Hint
Explanation of the sample:
Here's a graphical schedule for this output:
Other outputs using the same number of stalls are possible.
Source
USACO 2006 February Silver
一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作。给你每头奶牛的指定时间的区间,问你最小需要多少机器。
先按起始时间从小到大排序,起始时间相同则按结束时间从小到大排序
然后用一个优先队列来维护,vis记录奶牛的工作机器
View Code
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
一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作。给你每头奶牛的指定时间的区间,问你最小需要多少机器。
先按起始时间从小到大排序,起始时间相同则按结束时间从小到大排序
然后用一个优先队列来维护,vis记录奶牛的工作机器
#include<iostream> #include<cstdio> #include<cstring> #include<set> #include<map> #include<algorithm> #include<queue> using namespace std; #define N 60000 int n; int vis ; struct Node { int l,r; int pos; bool friend operator < (Node a,Node b) { if(a.r!=b.r) return a.r>b.r; } }p ; bool cmp(Node a,Node b) { if(a.l!=b.l) return a.l<b.l; return a.r<b.r; } int main() { while(scanf("%d",&n)==1) { memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { scanf("%d%d",&p[i].l,&p[i].r); p[i].pos=i; } sort(p,p+n,cmp); int ans=1; priority_queue<Node>q; q.push(p[0]); vis[p[0].pos]=ans; for(int i=1;i<n;i++) { Node tmp=q.top(); if(tmp.r<p[i].l){ q.pop(); vis[p[i].pos]=vis[tmp.pos]; } else{ ans++; vis[p[i].pos]=ans; } q.push(p[i]); } printf("%d\n",ans); for(int i=0;i<n;i++) printf("%d\n",vis[i]); } return 0; }
View Code
相关文章推荐
- js面向对象之选项卡的实现
- /bin、/sbin、/usr/bin、/usr/sbin目录的区别
- 最后一个1【进制转换】
- makefile编写例程
- Oracle主键自增长及自动生成策略
- C语言一个细节地方的说明【防止使用不当而出错】
- iOS判断设备-userInterfaceIdiom
- 浅谈 SOAP
- UVa 10082 WERTYU
- 关于类的成员的访问权限的题目
- 自动寻路NavMeshAgent的使用
- Memcached之缓存雪崩,缓存穿透,缓存预热,缓存算法(7)
- logistic回归
- C语言之指针
- GitHub 第一坑:换行符自动转换
- 字符串通配符
- 不重叠的线段
- HDU1556-----Color the ball
- 华为OJ初级题中多线程
- iOS程序员面试题系列(1)