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

题目链接POJ3190

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 reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows.

Help FJ by determining:
The minimum number of stalls required in the barn so that each cow can have her private milking period
An assignment of cows to these stalls over time
Many answers are correct for each test dataset; a program will grade your answer.
Input

Line 1: A single integer, N

Lines 2…N+1: Line i+1 describes cow i’s milking interval with two space-separated integers.
Output

Line 1: The minimum number of stalls the barn must have.

Lines 2…N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.
Sample Input

5
1 10
2 4
3 6
5 8
4 7
Sample Output

4
1
2
3
2
4
Hint

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.
题目思路

两个结构体,一个排序方法,一个主函数
第一个结构体存储奶牛的开始产奶时间和产奶结束时间以及奶牛的编号,
排序方法是将奶牛按开始产奶的时间从小到大排序.第二个结构体是存储优先队列的(emmm可能表述不对),
优先级为产奶结束时间早的奶牛	

AC代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#define ll long long
#define dd double
using namespace std;

struct node {				//存储奶牛开始产奶时间、结束产奶时间,和奶牛的编号;
	ll id;
	ll begin, end;
}cow[50100];

bool cmp(node a, node b) {	//将奶牛,按产奶时间从小到大排序
	if (a.begin < b.begin) {
		return true;
	}
	else {
		return false;
	}
}

struct point{				//优先队列
	ll end;
	ll id;
	friend bool operator < (point n1, point n2) {	//优先级为产奶结束时间早的奶牛
		if (n1.end > n2.end) {
			return true;
		}
		else {
			return false;
		}
	}
	point(ll e,ll id1):end(e),id(id1){ }			//传送数据......
};

int main() {
	ios::sync_with_stdio(0);			//取消cin,cout与stdio的同步,防止代码时间超限
	ll n;
	while (cin >> n) {
		ll pos[50100];					//用于存储每只奶牛去的栏位
		for (ll i = 0; i < n; i++) {	//输入每只奶牛的开始以及结束时间
			cin >> cow[i].begin >> cow[i].end;
			cow[i].id = i;				//id设置为该奶牛的编号
		}
		sort(cow, cow + n, cmp);		//按cmp的排序方法排序
		priority_queue<point> pq;		//设置优先队列
		ll total = 0;					//用来标记牛的去向以及需要的最少位置数


		for (ll i = 0; i < n; i++) {	//枚举每一头牛
			if (pq.empty()) {			//如果队列是空的
				total++;				//这里之所以先去进行total++,是因为每头牛的编号是从1开始的
				pq.push(point(cow[i].end, total));//加入队列
				pos[cow[i].id] = total; //存储位置
			}
			else {						//如果队列不是空的
				point st = pq.top();	//把队列优先级最高的那个和这头牛去比较
				if (cow[i].begin > st.end) {//如果这头牛的开始时间要比队列中结束时间最早的牛要早
					pq.pop();				//把队列中的这头牛丢出去
					pos[cow[i].id] = st.id; //把这头牛的id设置为被丢出去的牛的id
					pq.push(point(cow[i].end, st.id));//然后把这头牛的结束时间和id压入队列
				}
				else {						//否则
					total++;				//增加一个栏位总数
					pq.push(point(cow[i].end, total));//将这头牛压入队列
					pos[cow[i].id] = total;//存入这头牛去的地方
				}
			}
		}
		cout << total << endl;				//输出最少同时需要多少栏位
		for (ll i = 0; i < n; i++) {		//枚举每一头牛
			cout << pos[i] << endl;			//输出它们的位置
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值