这是个典型的线程服务区间模型。一些程序要在一段时间区间上使用一段线程运行,问至少要使用多少线程来为这些程序服务?

  把所有程序以左端点为第一关键字,右端点为第二关键字从小到大排序。从左向右扫描。处理当前区间时,提取出所有线程中最后一个被服务中的区间中右端点最小的区间(可用小根堆实现),若当前区间左端点值大于提取出的区间的右端点的值,则把当前区间安排到选中的区间的那个线程,否则只能再派出一个线程来负责该区间了。

  此贪心是正确的,因为正在被服务中的区间中右端点最小的区间,能使当前区间不被该线程负责的当前区间左端点范围最小。

  注意:

  1. 右端点为第二关键字。
  2. 题中的时间点其实不是点,而是个格子,所以是“当前区间左端点值【大于】提取出的区间的右端点的值”,而不是【大于等于】。
#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 贪心的更多相关文章

  1. poj3190 Stall Reservations (贪心+优先队列)

    Cleaning Shifts Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  2. POJ 3190 Stall Reservations贪心

    POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...

  3. [USACO06FEB] Stall Reservations 贪心

    [USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...

  4. POJ--3190 Stall Reservations(贪心排序)

    这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...

  5. POJ3190 Stall Reservations 【贪婪】

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3106   Accepted: 111 ...

  6. POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)

    Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...

  7. poj3190 Stall Reservations(贪心+STL)

    https://vjudge.net/problem/POJ-3190 cin和scanf差这么多么..tle和300ms 思路:先对结构体x升序y升序,再对优先队列重载<,按y升序. 然后依次 ...

  8. poj 3190 Stall Reservations 贪心 + 优先队列

    题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...

  9. poj3190 Stall Reservations

    我一开始想用线段树,但是发现还要记录每头cow所在的棚...... 无奈之下选择正解:贪心. 用priority_queue来维护所有牛棚中结束时间最早的那个牛棚,即可得出答案. 注意代码实现的细节. ...

随机推荐

  1. Run as ant build每次都执行两次-问题解决

    在Eclipse里面,运行ant,整个测试流程总是执行两遍,其几天试了下在DOS命令行直接调用ant, 结果发现只执行了一次,并且内存消耗好像也没那么大了,估计是eclipse自己的问题.问题解决了, ...

  2. javascript 核心概念(1)-数据类型

    语法 (1)到现在为止,大多数浏览器也还是支持到ECMAScript 第三版的标准. 核心概念就是一个语言的基本工作原理,涉及语法,操作符,数据类型. (2)javascript的一切--变量,函数名 ...

  3. ie9长度兼容

    onchange="this.value=this.value.substring(0, 10)" onkeydown="this.value=this.value.su ...

  4. 【SQL】CASE与DECODE

    1. case..when case..when语句用于按照条件返回查询结果,如当我们想把emp表的工资按照多少分成几个不同的级别,并分别统计各个级别的员工数.SQL语句如下: select (cas ...

  5. oracle 命令记录

    监听程序启动停止查看名利: 1.切换到oracle用户:su - oracle 2.查看监听状态:lsnrctl status 3.停止监听:lsnrctl stop 4.启动监听:lsnrctl s ...

  6. Assembly之instruction之JUMP

    JMP  Jump unconditionally Syntax   JMP  label Operation PC + 2 × offset −> PC Description The 10- ...

  7. I2C controller core之Bit controller(03)

    FPGA proven, AISC proven, I2C controller core from OpenCores http://opencores.org/project,i2c Bit-co ...

  8. In Swift, typedef is called typealias:

    It is used to create an alias name for another data type. The syntax of the typedef declaration is:[ ...

  9. PAT_A1150#Travelling Salesman Problem

    Source: PAT A1150 Travelling Salesman Problem (25 分) Description: The "travelling salesman prob ...

  10. 58.fetch phbase

    1.fetch phbase工作流程 The coordinating node identifies which documents need to be fetched and issues a ...