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;
}

  

原文地址:https://www.cnblogs.com/headboy2002/p/9125557.html

时间: 2024-10-09 19:04:28

POJ3190 Stall Reservations 贪心的相关文章

POJ 3190 Stall Reservations(贪心+优先队列优化)

Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reserv

POJ 3190 Stall Reservations贪心

POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obvi

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

Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a

POJ3190 Stall Reservations 【贪婪】

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3106   Accepted: 1117   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time in

poj3190 Stall Reservations(贪心+STL)

https://vjudge.net/problem/POJ-3190 cin和scanf差这么多么..tle和300ms 思路:先对结构体x升序y升序,再对优先队列重载<,按y升序. 然后依次入队,如果node[i].x<=q.top().y ans++, 否则出队,入队,使用出队的那个摊位. 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring>

POJ 3190 Stall Reservations(贪心)

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3590   Accepted: 1284   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time in

Stall Reservations(POJ 3190 贪心+优先队列)

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4434   Accepted: 1588   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time in

Stall Reservations (poj 3190 贪心)

Language: Default Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3394   Accepted: 1215   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over so

poj3190Stall Reservations(贪心+优先队列)

题目链接: 啊哈哈,点我点我 思路: 首先根据挤奶时间的先后顺序排序...然后将第一头牛加入优先队列..然后就是加入优先队列的牛应该根据越早结束挤奶那么优先级更高,如果时间结束点相等,那么开始时间早的优先级高... 然后从前向后枚举.如果碰到有牛的挤奶时间的开始值大于优先队列的首部的结束值,那么说明这两头牛可以一起公用一个挤奶房..然后从优先队列中删除这头牛..那么这个问题就得到解决了... 题目: Language: Default Stall Reservations Time Limit: