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. 利用InstallShiled 10.5制作AE应用程序安装包

    [转]利用InstallShiled 10.5制作AE应用程序安装包 作者:3SNEWS 社区ESRI(ArcGIS)版版主:zhaoxiang_whuhttp://www.3snews.net/bb ...

  2. 【重大更新】DevExpress v17.2新版亮点—WPF篇(三)

    DevExpress年终击穿底价,单套授权低至67折!仅剩最后10天!查看详情>>> 用户界面套包DevExpress v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增 ...

  3. <constant name="struts.devMode" value="false" />

    在开发中,我们常常会遇到<constant name="struts.devMode" value="false" />,这是struts2的特性, ...

  4. C语言基础:常见循环语句 分类: iOS学习 c语言基础 2015-06-10 21:46 13人阅读 评论(0) 收藏

    for语句 for( 初始化表达式; 循环判断条件  ;增量表达式); while(条件表达式){ 循环体; } 先判断条件表达式,如果为真就执行循环体,执行完再去判断条件表达式 do{ 循环体; } ...

  5. rem & em初探

    Rem为单位 CSS3的出现,他同时引进了一些新的单位,包括我们今天所说的rem.在W3C官网上是这样描述rem的——“font size of the root element” .下面我们就一起来 ...

  6. SpringAop与AspectJ

    AspectJ AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件. spring ao ...

  7. pandas.read_csv 参数 index_col=0

    index_col : int or sequence or False, default None 用作行索引的列编号或者列名,如果给定一个序列则有多个行索引. 如果文件不规则,行尾有分隔符,则可以 ...

  8. 30秒让让你的电脑快一倍 - 计算机基础 - 中国红客联盟 - Powered

    一.清理垃圾 在Windows在安装和使用过程中都会产生相当多的垃圾文件,包括临时文件(如:*.tmp.*._mp)日志文件(*.log).临时帮助文件(*.gid).磁盘检查文件(*.chk).临时 ...

  9. fiddler Android抓包与弱网

    tools rules-ctrl+R 搜索 oSession["request-trickle-delay"] = rules-perfromance-simulate modem ...

  10. CF1143F/1142C U2

    CF1143F/1142C U2 巧妙的思维题.注意到这里只用两个点就可以确定一根抛物线,联想到两点确定一条直线,尝试转化. \(y=x^2+bx+c\) 就可以写成 \(y-x^2=bx+c\) , ...