POJ3190 Stall Reservations 贪心
这是个典型的线程服务区间模型。一些程序要在一段时间区间上使用一段线程运行,问至少要使用多少线程来为这些程序服务?
把所有程序以左端点为第一关键字,右端点为第二关键字从小到大排序。从左向右扫描。处理当前区间时,提取出所有线程中最后一个被服务中的区间中右端点最小的区间(可用小根堆实现),若当前区间左端点值大于提取出的区间的右端点的值,则把当前区间安排到选中的区间的那个线程,否则只能再派出一个线程来负责该区间了。
此贪心是正确的,因为正在被服务中的区间中右端点最小的区间,能使当前区间不被该线程负责的当前区间左端点范围最小。
注意:
- 右端点为第二关键字。
- 题中的时间点其实不是点,而是个格子,所以是“当前区间左端点值【大于】提取出的区间的右端点的值”,而不是【大于等于】。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int MAX_RANGE_CNT = 50010; struct Line
{
int End, Id; bool operator < (const Line a) const
{
return End > a.End;
} Line() {}
Line(int id, int end):Id(id),End(end){} };
priority_queue<Line> Heap; struct Range
{
int L, R, TargetLineId;
}_ranges[MAX_RANGE_CNT], *_sortedRanges[MAX_RANGE_CNT]; bool cmp(Range *a, Range *b)
{
if (a->L != b->L)
return a->L < b->L;
else
return a->R < b->R;
} int main()
{
int rangeCnt;
scanf("%d", &rangeCnt);
for (int i = 1; i <= rangeCnt; i++)
scanf("%d%d", &_ranges[i].L, &_ranges[i].R);
for (int i = 1; i <= rangeCnt; i++)
_sortedRanges[i] = _ranges + i;
sort(_sortedRanges + 1, _sortedRanges + rangeCnt + 1, cmp);
int lineCnt = 0;
for (int i = 1; i <= rangeCnt; i++)
{
Range *cur = _sortedRanges[i];
if (!Heap.empty() && Heap.top().End < cur->L)
{
Line prev = Heap.top();
cur->TargetLineId = prev.Id;
Heap.pop();
Heap.push(Line(prev.Id,cur->R));
}
else
{
lineCnt++;
cur->TargetLineId = lineCnt;
Heap.push(Line(lineCnt, cur->R));
}
}
printf("%d\n", lineCnt);
for (int i = 1; i <= rangeCnt; i++)
printf("%d\n", _ranges[i].TargetLineId);
return 0;
}
POJ3190 Stall Reservations 贪心的更多相关文章
- poj3190 Stall Reservations (贪心+优先队列)
Cleaning Shifts Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- 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(贪心排序)
这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...
- POJ3190 Stall Reservations 【贪婪】
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3106 Accepted: 111 ...
- POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)
Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...
- poj3190 Stall Reservations(贪心+STL)
https://vjudge.net/problem/POJ-3190 cin和scanf差这么多么..tle和300ms 思路:先对结构体x升序y升序,再对优先队列重载<,按y升序. 然后依次 ...
- poj 3190 Stall Reservations 贪心 + 优先队列
题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...
- poj3190 Stall Reservations
我一开始想用线段树,但是发现还要记录每头cow所在的棚...... 无奈之下选择正解:贪心. 用priority_queue来维护所有牛棚中结束时间最早的那个牛棚,即可得出答案. 注意代码实现的细节. ...
随机推荐
- B - Magnets
Problem description Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't ...
- vue 使用localStorage保存页面变量到浏览器变量中
const STORAGE_KEY = 'todos-vuejs'//定义常量保存键值 export default{ fetch(){ return JSON.parse(window.localS ...
- VS2015 右侧导航插件地址
右侧导航插件: https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.ProductivityPowe ...
- 5.20rieds切换数据库
- 【转】虚拟化(三):vsphere套件的安装注意及使用
vsphere套件里面主要的组件有esxi.vcenter server .vsphere client和vsphere web client. vcenter做为vsphere套件的核心管理成员之一 ...
- Python总结1
时间:24日下午输入:input()输出:print()格式化输出通过某种占位符用于替换字符串中某个位置的字符.占位符:%s:可以替换任意类型%d:可以替换数字类型 需要掌握的#1.strip去左右两 ...
- AD 域服务简介(一)- 基于 LDAP 的 AD 域服务器搭建及其使用(转)
一.前言 1.1 AD 域服务 什么是目录(directory)呢? 日常生活中使用的电话薄内记录着亲朋好友的姓名.电话与地址等数据,它就是 telephone directory(电话目录):计算机 ...
- PAT_A1153#Decode Registration Card of PAT
Source: PAT A1153 Decode Registration Card of PAT (25 分) Description: A registration card number of ...
- PAT_A1127#ZigZagging on a Tree
Source: PAT A1127 ZigZagging on a Tree (30 分) Description: Suppose that all the keys in a binary tre ...
- Python 非空即真、列表生成式、三元表达式 day3
一.非空即真: Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false 布尔型,False表示False,其他为True 整数和浮点数,0表示False,其他为 ...