POJ 3190 Stall Reservations 【贪心 优先队列】
题意:给出n头牛必须单独占用一台机器的时间段,问至少需要多少台机器
先按照每头牛的时间的x来排序,然后用一个优先队列(优先选取最小的)维护已经喂好的牛的最小的结束时间
比如现在优先队列里面有m头牛已经完成了饲喂,
对于第m+1头牛, 如果它的开始时间小于这m头牛里面的最小的结束时间,那么必须增加一台机器,同时把第m+1头牛放进队列
如果它的开始时间大于这m头牛里面的最小的结束时间,把当前队头弹出,再把m+1头牛放进去
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; struct node{
int x,y,id;
bool operator < (const node &rsh) const {
return rsh.y < y;
}
} a[maxn]; int cmp(node n1,node n2){
return n1.x < n2.x;
} int ans[maxn]; int n; int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) {
scanf("%d %d",&a[i].x,&a[i].y);
a[i].id=i;
}
sort(a+,a+n+,cmp);
priority_queue<node> q;
q.push(a[]);
ans[a[].id]=;
int cnt=; for(int i=;i<=n;i++){
node tmp=q.top();
if(a[i].x <= tmp.y){
ans[a[i].id]=++cnt;
q.push(a[i]);
}
else{
q.pop();
q.push(a[i]);
ans[a[i].id] = ans[tmp.id];
} } printf("%d\n",cnt);
for(int i=;i <= n;i++)
printf("%d\n",ans[i]); return ;
}
POJ 3190 Stall Reservations 【贪心 优先队列】的更多相关文章
- poj 3190 Stall Reservations 贪心 + 优先队列
题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- 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 (优先队列)C++
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7646 Accepted: 271 ...
- poj 3190 Stall Reservations
http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- POJ 3190 Stall Reservations【贪心】
POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...
- POJ -3190 Stall Reservations (贪心+优先队列)
http://poj.org/problem?id=3190 有n头挑剔的奶牛,只会在一个精确时间挤奶,而一头奶牛需要占用一个畜栏,并且不会和其他奶牛分享,每头奶牛都会有一个开始时间和结束时间,问至少 ...
- poj3190 Stall Reservations (贪心+优先队列)
Cleaning Shifts Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- [USACO06FEB] Stall Reservations 贪心
[USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...
随机推荐
- child和childNodes的区别
child和childNodes区别: childNodes是标准属性, child是非标准属性 childNodes: 获取节点,不同浏览器表现不同 IE 只获取元素节点 非IE 获取元素节点和文本 ...
- getElementsByClassName 方法兼容性封装方法二
var getElmsByClsName = function(className, results) { results = results || []; // 判断浏览器是否支持 getEleme ...
- scp 命令简明介绍
安全复制(英语:Secure copy,缩写SCP)是指在本地主机与远程主机或者两台远程主机之间基于Secure Shell(SSH)协议安全地传输电脑文件."SCP"通常指安全复 ...
- python字符串、列表、元组
字符串的常用方法: name.count('h')统计h在name中出现的次数 name.find('h')查找h的索引 '?'.join(name)使用问好拼接 name.encode('gb231 ...
- css3新增的属性有哪些
徐先森讲web CSS3新增的属性有哪些: CSS 用于控制网页的样式和布局. CSS3 是最新的 CSS 标准. CSS3新增了很多的属性,下面一起来分析一下新增的一些属性: 1.CSS3边框: b ...
- Python中zip()与zip(*)的用法
目录 Python中zip()与zip(*)的用法 zip() 知识点来自leetcode最长公共前缀 Python中zip()与zip(*)的用法 可以看成是zip()为压缩,zip(*)是解压 z ...
- 怎样验证layer.prompt输入的值为数值型???
JS中使用isNaN()判断layer.prompt输入的值为数值型,代码如下: layer.prompt({ title: '设置比值', }, function(value, index, ele ...
- zabbix、agent端服务器图形化展示
[root@agent ~]# cat /etc/hostname agent.zabbix.com [root@agent ~]# cat /etc/hosts 127.0.0.1 localh ...
- 字符串时间转Date格式
/** * 字符串时间格式转 Date 格式 * @param strDate * @return */ public static Date getDateTimeByStringTime(Stri ...
- 嵌入式平台选择:树莓派 or BeagleBone Black(BBB)
原文链接: Michael Leonard 翻译: 极客范- 小道空空 译文链接: http://www.geekfan.net/5246/ 嵌入式平台选择:树莓派 or BeagleBone Bla ...