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 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.

题意:

在这里插入图片描述

思路分析:

当一头牛在一个栅栏里挤完奶之后,这个栅栏就处于开放状态,其他的牛就可以进去了。
如果一头牛想要挤奶,但是当前所有的栅栏都满了,就要新开一个栅栏;如果没满,牛就可以进去。
也就是说,一个基本思路是遍历牛的起始时间(准备进去的时间),然后找一下有没有空的栅栏。如果有,牛就进去;如果没有,就新开一个。因此我们先对牛的起始时间进行排序,确保我们遍历的牛是按时间顺序进来的。
现在的关键就在于 “寻找空的栅栏” 这个步骤怎么实现。如果每来一头牛我们都要遍历全部的栅栏,时间复杂度就在O(n^2)了。这里我们可以用贪心的思维想一下:我们要想知道有没有空的栅栏,只需要知道最早结束的栅栏是否为空,也就是这个栅栏的结束时间小于这头牛的起始时间。每次我们都用最早结束时间的栅栏和当前牛进行处理就可以推出全局最优解。
因此每遍历一头牛我们都需要知道最早结束栅栏的结束时间,也就是时刻都需要在所有栅栏中获取一个结束时间的最小值。这里可以想到用最小堆来实现。
因此,对于牛的节点有:开始时间(s),结束时间(e),栅栏编号(id2). 对于栅栏的节点有:栅栏编号(id),结束时间(e).
由于我们输出数据是按照原来的牛顺序输出,因此我们还需要在牛的节点里加一个牛最初的编号(id).
我们先对牛进行起始时间排序,然后遍历牛,与最小堆判断,再进行相应处理。最后,按照牛最初的编号从小到大排回去,输出数据。

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm> 
using namespace std;

//至少需要多少个畜栏才能完成全部挤奶操作
//对奶牛按照开始时间升序排序
//奶牛存编号,开始时间和结束时间,栅栏编号 
//栅栏存结束时间和编号 
//用最小堆获取最早结束的栅栏
//之后按奶牛的编号排序,输出栅栏编号

struct cow{
	int id, id2;
	int s, e;
}; 

struct stall{
	int id, e;
	bool operator < (const stall &b) const{
		return e > b.e;
	}
};

bool cmp1(const cow &a, const cow &b)
{
	return a.s < b.s;
}

bool cmp2(const cow &a, const cow &b)
{
	return a.id < b.id;
}

int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		vector<cow> a;
		cow t;
		stall tt;
		for(int i = 0;i < n;i++)
		{
			t.id = i;
			scanf("%d%d",&t.s, &t.e);
			a.push_back(t);
		}
		sort(a.begin(), a.end(), cmp1);
		priority_queue<stall> pq;
		int cnt = 0;
		for(int i = 0;i < n;i++)
		{
			if(pq.empty())
			{
				cnt++;
				a[i].id2 = cnt;
				tt.id = cnt, tt.e = a[i].e;
				pq.push(tt);
				continue;
			}
			if(pq.top().e < a[i].s) //有栅栏空着 
			{
				tt.e = a[i].e, tt.id = pq.top().id, a[i].id2 = tt.id;
				pq.pop();
				pq.push(tt);
			}
			else //栅栏都满了 
			{
				cnt++;
				tt.e = a[i].e, tt.id = cnt, a[i].id2 = tt.id;
				pq.push(tt);
			}
		} 
		sort(a.begin(), a.end(), cmp2);
		printf("%d\n",cnt);
		for(int i = 0;i < n;i++)
		{
			printf("%d\n",a[i].id2);
		}
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值