POJ 3190 Stall Reservations 贪心+优先队列

Stall Reservations

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 10476

 

Accepted: 3702

 

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

扫描二维码关注公众号,回复: 2406891 查看本文章

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.

Source

USACO 2006 February Silver

算法分析:

题意:

   n头母牛都有固定的挤奶时间段,问至少需要多少挤奶站才能让n头母牛挤奶成功。

分析:

  因为每一头母牛都有固定的挤奶时间,第一,时间开始得早的母牛肯定先挤奶,所以我们按照开始时间对其排序。第二,时间结束的早的母牛可以让出挤奶台,所以我们将正在挤奶的母牛压入一个优先队列(结束时间早在队首)。这样循环放入,得到最优解。

  思想:贪心+优先队列

 

代码实现:

#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cctype>  
#include<cmath>  
#include<iostream>  
#include<sstream>  
#include<iterator>  
#include<algorithm>  
#include<string>  
#include<vector>  
#include<set>  
#include<map>  
#include<stack>  
#include<deque>  
#include<queue>  
#include<list>  
using namespace std;  
const double eps = 1e-8;  
typedef long long LL;  
typedef unsigned long long ULL;  
const int INT_INF = 0x3f3f3f3f;  
const int INT_M_INF = 0x7f7f7f7f;  
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;  
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;  
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};  
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};  
const int MOD = 1e9 + 7;  
const double pi = acos(-1.0);  
const int MAXN = 1e5 + 10;  
const int MAXT = 10000 + 10;  
const int M=50010;
struct C
{
	int p,l,r;
	bool operator<(const C &c)const
	{
	  if(r==c.r)
	   return l>c.l;
	  else 
	  return r>c.r;
	}
};
bool cmp(const C &a,const C &b)
{
	if(a.l!=b.l)
	return a.l<b.l;
	else 
	return a.r<b.r;
	
}
int main()
{
	int n;
	while (scanf("%d",&n)!=EOF)
	{
		C c[M];
		priority_queue<C> q;     //结束时间早在队首
		int u[M];                 //表示i个机器用第几个摊位
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&c[i].l,&c[i].r);
			 c[i].p=i+1;
		}
		sort(c,c+n,cmp);   //开始时间从小到大排序
		q.push(c[0]);
		u[c[0].p]=1;
		int ans=1;
		for(int i=1;i<n;i++)
		{
			if(!q.empty()&&q.top().r<c[i].l)  //可以加入当前摊位
			{
				u[c[i].p]=u[q.top().p];
				q.pop();
			}
			else                              //需要另外添加摊位
			{
				ans++;
				u[c[i].p]=ans;
			}
			q.push(c[i]);
		}
		printf("%d\n",ans);
		for(int i=1;i<=n;i++)
			printf("%d\n",u[i]);
		
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdz20172133/article/details/81216985