poj3190 Stall Reservations (贪心+优先队列)
Cleaning Shifts
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 9 Accepted Submission(s) : 2
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.
<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.
to hire or -1 if it is not possible to assign a cow to each shift.
1 7
3 6
6 10
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 (贪心+优先队列)的更多相关文章
- poj 3190 Stall Reservations 贪心 + 优先队列
题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...
- POJ3190 Stall Reservations 贪心
这是个典型的线程服务区间模型.一些程序要在一段时间区间上使用一段线程运行,问至少要使用多少线程来为这些程序服务? 把所有程序以左端点为第一关键字,右端点为第二关键字从小到大排序.从左向右扫描.处理当前 ...
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- [USACO06FEB] Stall Reservations 贪心
[USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...
- POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)
Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...
- POJ--3190 Stall Reservations(贪心排序)
这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...
- POJ 3190 Stall Reservations (优先队列)C++
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7646 Accepted: 271 ...
- POJ3190 Stall Reservations 【贪婪】
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3106 Accepted: 111 ...
- poj3190 Stall Reservations(贪心+STL)
https://vjudge.net/problem/POJ-3190 cin和scanf差这么多么..tle和300ms 思路:先对结构体x升序y升序,再对优先队列重载<,按y升序. 然后依次 ...
随机推荐
- js解码编码decodeURI与decodeURIComponent区别
###decodeURI与decodeURIComponent区别 1. 概念: URI: Uniform ResourceIdentifiers,通用资源标识符 Global对象的encodeURI ...
- bjui的validate表单验证的使用
date-rule ="date" 表示格式为yyyy-MM-dd date-rule = "datetime" 表示格式为yyyy-MM-dd HH:mm:s ...
- 你应该关注的几个Eclipse超酷插件
来自非营利性Eclipse基金会的Eclipse IDE以其插件生态系统著称.Eclipse市场拥有海量插件可供下载,你可以通过插件定制自己的Eclipse. 最近我在Eclipse Marketpl ...
- anu - pureComponent
import { inherit } from "./util"; import { Component } from "./Component"; impor ...
- tomcat部署和启动3
点击STATUES,弹出登陆框 You are not authorized to view this page. If you have not changed any configuration ...
- SQL SERVER 压缩数据库日志文件
ALTER DATABASE DBNAME SET RECOVERY SIMPLE --设置为简单恢复模式 GO DBCC SHRINKFILE (DBNAME_log, 1) GO ALTER DA ...
- 批处理设置IP地址 - imsoft.cnblogs
批处理设置IP地址 不知朋友们是否有这样的经历,把本本带到单位上网时,由于单位需要配固定IP地址,而家里是自动获得IP地址的,所以每天都要对这个IP地址设置来设置去,那么有没有简单方便的办法呢?其实我 ...
- HTTPS网站的内幕
什么是HTTPS网站? HTTPS可以理解为HTTP+TLS,HTTP是互联网中使用最为广泛的协议,目前大部分的WEB应用和网站都是使用HTTP协议传输. 那网站为什么要实现HTTPS? 一言概之,为 ...
- Knight Tournament 合并区间
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...
- 【C#】 增加多个分部类
有时需要在一个类下面增加多个不同功能的分部类,或者是不同开发组员以其命名的分部类. eg: 首先创建一个类,改为分部类,partial.. 复制此类的文件,改一个文件名.然后修改项目文件.csproj ...