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

Source

USACO 2006 February Silver

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define epst 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 50005

struct stud{
  int x,y;
  int pos;
  bool operator <(const stud d) const
  {
		return y>d.y;
  }
}f[N];

int ans[N];
int k;
priority_queue<stud>q;
int n;

int cmp(stud a,stud b)
{
	if(a.x==b.x) return a.y<b.y;
	return a.x<b.x;
}

void solve()
{
	int i,j;
	stud cur;
	while(!q.empty())q.pop();
	for(i=1;i<=n;i++)
	{
		if(q.empty())
		{
			ans[f[i].pos]=++k;
			q.push(f[i]);
			continue;
		}
        cur=q.top();
        if(cur.y<f[i].x)
		{
			ans[f[i].pos]=ans[cur.pos];
			q.pop();
		}
        else  ans[f[i].pos]=++k;
        q.push(f[i]);
	}
}

int main()
{
	int i,j;
	while(~sf(n))
	{
		for(i=1;i<=n;i++)
			{
				sff(f[i].x,f[i].y);
			    f[i].pos=i;
			}
		sort(f+1,f+n+1,cmp);
		k=0;
		solve();
		pf("%d\n",k);
		for(i=1;i<=n;i++)
			pf("%d\n",ans[i]);
	}
    return 0;
}
时间: 2024-08-08 01:15:04

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

POJ 3190 Stall Reservations【贪心】

POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序,然后维护一个优先队列,里面以已经开始挤奶的奶牛的结束时间早为优先.然后每次只需要检查当前是否有奶牛的挤奶工作已经完成的机器即可,若有,则换那台机器进行工作.若没有,则加一台新的机器.注意:利用priority_queue<Node,vector<Node>,less<Node>

POJ 3190 Stall Reservations (优先队列)C++

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7646   Accepted: 2710   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

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

http://poj.org/problem?id=3190 有n头挑剔的奶牛,只会在一个精确时间挤奶,而一头奶牛需要占用一个畜栏,并且不会和其他奶牛分享,每头奶牛都会有一个开始时间和结束时间,问至少需要多少个 畜栏  并且输出奶牛 i 在哪个畜栏 内挤奶. 首先应该对奶牛以开始时间从小到大排序,然后每次在开始的奶牛中选择结束时间最小的奶牛,这就需要用优先队列.看是否可以用这个畜栏,可以就加入到队列中,不可以就重新加一个畜栏. 1 #include <iostream> 2 #include

POJ 3190 Stall Reservations

http://poj.org/problem?id=3190 //最初思路 按数组排序 也好 堆维护也好 都是想 让开始时间 > 结束时间的牛合并 然后最后求出 个数就好//但是这样无法求得每头牛的分配过程//所以 模拟 加贪心 堆模拟正在挤奶的机器 按结束时间从小到大排列 没搜索加入一头牛 就结束一头牛的挤奶//这样可以求得每头牛 在哪个机器挤奶 并且如果 新加入牛时 这头牛已经结束 那么它就可以接着使用 否则 只能新加一台机器//然后stall就是 进一头牛 确定一个的stall 这样就是纯

POJ 3190 Stall Reservations-奶牛分栏(区间贪心,优先队列)

http://poj.org/problem?id=3190 题目大意:每一只奶牛要求在时间区间[A,B]内独享一个牛栏.问最少需要多少个牛栏. 贪心策略是优先满足A最小的奶牛,维持一个牛栏B最小堆,将新来的奶牛塞进B最小的牛栏里. <p><span style="color: rgb(51, 51, 51); font-family: 'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif; line

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 贪心

这是个典型的线程服务区间模型.一些程序要在一段时间区间上使用一段线程运行,问至少要使用多少线程来为这些程序服务? 把所有程序以左端点为第一关键字,右端点为第二关键字从小到大排序.从左向右扫描.处理当前区间时,提取出所有线程中最后一个被服务中的区间中右端点最小的区间(可用小根堆实现),若当前区间左端点值大于提取出的区间的右端点的值,则把当前区间安排到选中的区间的那个线程,否则只能再派出一个线程来负责该区间了. 此贪心是正确的,因为正在被服务中的区间中右端点最小的区间,能使当前区间不被该线程负责的当