Cleaning Shifts

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 2
Problem Description
Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T.

Each cow is only available at some interval of times during the day for
work on cleaning. Any cow that is selected for cleaning duty will work for the
entirety of her interval.

Your job is to help Farmer John assign some
cows to shifts so that (i) every shift has at least one cow assigned to it, and
(ii) as few cows as possible are involved in cleaning. If it is not possible to
assign a cow to each shift, print -1.

 
Input
* Line 1: Two space-separated integers: N and T
<br> <br>* Lines 2..N+1: Each line contains the start and end times
of the interval during which a cow can work. A cow starts work at the start time
and finishes after the end time.
 
Output
* Line 1: The minimum number of cows Farmer John needs
to hire or -1 if it is not possible to assign a cow to each shift.
 
Sample Input
3 10
1 7
3 6
6 10
 
Sample Output
2
 

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.

思路:

首先根据挤奶时间的先后顺序排序。。。然后将第一头牛加入优先队列。。然后就是加入优先队列的牛应该根据越早结束挤奶那么优先级更高,如果时间结束点相等,那么开始时间早的优先级高。。。

然后从前向后枚举。如果碰到有牛的挤奶时间的开始值大于优先队列的首部的结束值,那么说明这两头牛可以一起公用一个挤奶房。。然后从优先队列中删除这头牛。。那么这个问题就得到解决了。。。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = + ;
int order[maxn]; struct Node
{
int st, en, pos;
friend bool operator<(Node a, Node b)
{
if (a.en == b.en)
return a.st<b.st;
return a.en>b.en;
}
}node[maxn]; bool cmp(Node a, Node b)
{
if (a.st == b.st)
return a.en<b.en;
else
return a.st<b.st;
} priority_queue<Node>Q; int main()
{
int n, ans;
while (~scanf("%d", &n))
{
for (int i = ; i <= n; i++)
{
scanf("%d%d", &node[i].st, &node[i].en);
node[i].pos = i;
}
sort(node + , node + + n, cmp);
ans = ;
Q.push(node[]);
order[node[].pos] = ;
for (int i = ; i <= n; i++)
{
if (!Q.empty() && Q.top().en<node[i].st)
{
order[node[i].pos] = order[Q.top().pos];
Q.pop();
}
else
{
ans++;
order[node[i].pos] = ans;
}
Q.push(node[i]);
}
printf("%d\n", ans);
for (int i = ; i <= n; i++)
printf("%d\n", order[i]);
while (!Q.empty()) Q.pop();
}
return ;
}

poj3190 Stall Reservations (贪心+优先队列)的更多相关文章

  1. poj 3190 Stall Reservations 贪心 + 优先队列

    题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...

  2. POJ3190 Stall Reservations 贪心

    这是个典型的线程服务区间模型.一些程序要在一段时间区间上使用一段线程运行,问至少要使用多少线程来为这些程序服务? 把所有程序以左端点为第一关键字,右端点为第二关键字从小到大排序.从左向右扫描.处理当前 ...

  3. POJ 3190 Stall Reservations贪心

    POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...

  4. [USACO06FEB] Stall Reservations 贪心

    [USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...

  5. POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)

    Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...

  6. POJ--3190 Stall Reservations(贪心排序)

    这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...

  7. POJ 3190 Stall Reservations (优先队列)C++

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7646   Accepted: 271 ...

  8. POJ3190 Stall Reservations 【贪婪】

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3106   Accepted: 111 ...

  9. poj3190 Stall Reservations(贪心+STL)

    https://vjudge.net/problem/POJ-3190 cin和scanf差这么多么..tle和300ms 思路:先对结构体x升序y升序,再对优先队列重载<,按y升序. 然后依次 ...

随机推荐

  1. Python 编程核心知识体系-模块|面向对象编程(三)

    模块 面向对象编程

  2. 官网下载的spring-framework的一些描述

    刚下载下来是这个文件夹:

  3. 12个有趣的 XSS Vector

    XSS Vector #1 <script src=/〱20.rs></script> URL中第二个斜杠在Internet Explorer下(测试于IE11)可被U+303 ...

  4. OC基础:类和对象 分类: ios学习 OC 2015-06-12 18:55 17人阅读 评论(0) 收藏

    OC:Objective-c     面向对象的c语言,简称obj-c或者OC OC和C的区别 1.OC是C语言的超集,OC是在C语言的基础上结合smalltalk的优点,开发出来的语言.oc兼容所有 ...

  5. 通过JS动态创建和删除HTML元素

    <script type="text/javascript" language="Javascript"> function InputOnBlur ...

  6. CentOS下设置MySQL的root各种密码 总结

    一.更改mysql密码常用的三种方法 大部分情况下,一般用户没有权限更改密码,只有申请了权限或root用户才可以更改密码: 1.方法1:用mysqladmin mysqladmin -u root p ...

  7. su:鉴定故障

    deepin中从普通用户切换到管理员时出现su:鉴定故障 使用命令:su root [mylinux@localhost ~]$ su root 密码: su: 鉴定故障 解决方法: 使用命令:sud ...

  8. MVC 模型 视图, 控制器 写 三级联动

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  9. python--selenium多线程执行用例实例/执行多个用例

    python--selenium多线程执行用例实例/执行多个用例 我们在做selenium测试的时候呢,经常会碰到一些需要执行多个用例的情况,也就是多线 程执行py程序,我们前面讲过单个的py用例怎么 ...

  10. JPEG文件格式

    格式:JFIF(JPEG档的交换格式)压缩:JPEG(灰阶影像压缩比约为10:1:彩色影像约为20:1)以JPEG文件格式保存的图像实际上是2个不同格式的混合物:JPEG格式规范本身,用来定义图像的压 ...