POJ 3190 Stall Reservation(贪心)

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

题意:

又是母牛的问题,有N只母牛,现在要安排它们挤奶。但是每个母牛都有一个挤奶的时间段,现在要你安排用最少的小隔间给母牛挤奶。

思路:

将所有母牛的挤奶时间按照开始时间升序排序,刚开始想要每次进行一个比较找出最早结束的母牛,但是因为数据过大超时了。后来看别人的算法使用优先队列,第一次用,当做练手了。(ps:优先队列http://blog.csdn.net/zhang20072844/article/details/10286997)设置一个优先队列,以结束时间越早优先级越高,每次比较是否存在比当前奶牛开始时间更早结束的母牛,有就还用那个stall,没有就再添一个stall;

注意点:

1.将元素放入队列是copy操作,之后对于元素的操作不会再影响队列里面的状态;

2.“which includes both times A and B. ”所以挤奶的时间边界不能重叠;

3.cin、cout效率太低,推荐使用scanf、printf,多么痛的领悟~~

样例:

3

1 2

2 3

3 4

——

2

1

2

1

——

4

1 4

2 3

3 5

4 7

——

3

1

2

3

2

代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#define MAX_N 50010
#define INF 100000000
using namespace std;
struct node{
    int s_t;
    int e_t;
    int pos;
    bool operator < (const node &other)const
    {
        if(this->e_t==other.e_t)
            return this->s_t > other.s_t;
        return (this->e_t) > (other.e_t);
    }
};
priority_queue cList;
int stall[MAX_N];
const bool cmp(node x,node y);
node cows[MAX_N];
int N,sum;
int main()
{
    scanf("%d",&N);
    for(int i=0;i


你可能感兴趣的:(贪心)