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. stringBuild置空方法

    参看连接:http://blog.csdn.net/roserose0002/article/details/6972391

  2. Linux内核分析-分析system_call中断处理过程

    姓名:江军 ID:fuchen1994 分析system_call中断处理过程 使用gdb跟踪分析一个系统调用内核函数(您上周选择那一个系统调用),系统调用列表参见http://codelab.shi ...

  3. 难度2:ASCII码排序

    ASCII码排序 难度:2描述: 输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符. 输入: 第一行输入一个数N,表示有N组测试数据.后面的N行输入多组数据,每组输入数据 ...

  4. echarts折线图柱状图的坐标轴的颜色及样式的设置

    基本用法请查看echarts官网. 一.图例legend的设置. 1.字体和颜色的设置 textStyle:{ fontSize:15, color:'#fff' } 2.样式的设置 legend: ...

  5. OC基础:类的扩展.协议 分类: ios学习 OC 2015-06-22 19:22 34人阅读 评论(0) 收藏

    //再设计一个类的时候,有些方法需要对外公开(接口),有些仅供内部使用. 类的扩展:为类添加新的特征(属性)或者方法 对已知类: 1.直接添加 2.继承(在其子类中添加实例变量和方法) 3.使用ext ...

  6. B+与B-树

    1 .B-树定义 B-树是一种平衡的多路查找树,它在文件系统中很有用. 定义:一棵m 阶的B-树,或者为空树,或为满足下列特性的m 叉树:⑴树中每个结点至多有m 棵子树:⑵若根结点不是叶子结点,则至少 ...

  7. iOS 去除警告 看我就够了

    你是不是看着开发过程中出现的一堆的警告会心情一阵烦躁,别烦躁了,看完此文章,消除警告的小尾巴. 一.SVN 操作导致的警告 1.svn删除文件后报错 ”xx“is missing from worki ...

  8. 如何释放vector变量

    std::vector<cv::Point> probp; std::vector<int> plabel; plabel.clear(); std::vector<in ...

  9. AFN不支持 "text/html" 的数据的问题:unacceptable content-type: text/html

    使用AFN若遇到这个错误信息 Request failed: unacceptable content-type: text/html bug原因: 这不是AFNetworking的问题 这是做Ser ...

  10. Boosting学习笔记(Adboost、GBDT、Xgboost)

    转载请注明出处:http://www.cnblogs.com/willnote/p/6801496.html 前言 本文为学习boosting时整理的笔记,全文主要包括以下几个部分: 对集成学习进行了 ...