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. 【DevExpress v17.2新功能预告】DevExtreme ASP.NET MVC新的强类型HTML Helpers

    在ASP.NET MVC中构建视图时,强类型HTML helpers非常有用.像@Html.TextBoxFor(m => m.FirstName)这样内置的Helper方法已经存在很长时间了, ...

  2. HTTP Header之Content-Type

    HTTP Header之Content-Type   目录 1. HTTP Header 2. 文件请求和接口请求 3. 几种 Content-Type 3.1 application/x-www-f ...

  3. Python 之 numpy 和 tensorflow 中的各种乘法(点乘和矩阵乘)

    点乘和矩阵乘的区别: 1)点乘(即“ * ”) ---- 各个矩阵对应元素做乘法 若 w 为 m* 的矩阵,x 为 m*n 的矩阵,那么通过点乘结果就会得到一个 m*n 的矩阵. 若 w 为 m*n ...

  4. 《利用Python进行数据分析》笔记---第5章pandas入门

    写在前面的话: 实例中的所有数据都是在GitHub上下载的,打包下载即可. 地址是:http://github.com/pydata/pydata-book 还有一定要说明的: 我使用的是Python ...

  5. TypeError: HashUpdate fail

    关于crypto的md5加密报错: 代码: var crypto = require('crypto'); var md5 = crypto.createHash('md5'); //crypto模块 ...

  6. vue2.0分页组件,

    pagination.vue <!-- 表格分页组件 --> <template> <nav class="boot-nav"> <ul ...

  7. 游标 cursor

    * mongo shell下支持JS代码,可以通过JS获取游标,进而获取数据操作结果. var cursor = db.class1.find() cursor.next() 获取下一条结果 curs ...

  8. 与众不同 制作会唱歌的WinRAR - imsoft.cnblogs

    为了使用方便,我们可能会把RAR压缩包制作成自解压文件.WinRAR自带的自解压模块虽然使用很方便,但千篇一律的外观看起来实在 乏味.其实,只要通过简单改造,你就可以制作出与众不同,声色俱佳的WinR ...

  9. LOJ2538. 「PKUWC2018」Slay the Spire【组合数学】

    LINK 思路 首先因为式子后面把方案数乘上了 所以其实只用输出所有方案的攻击力总和 然后很显然可以用强化牌就尽量用 因为每次强化至少把下面的牌翻一倍,肯定是更优的 然后就只有两种情况 强化牌数量少于 ...

  10. API设计风格(RRC、REST、GraphQL、服务端驱动)

    API设计风格(RRC.REST.GraphQL.服务端驱动) Web API设计其实是一个挺重要的设计话题,许多公司都会有公司层面的Web API设计规范,几乎所有的项目在详细设计阶段都会进行API ...