POJ 3190 Stall Reservations(任务调度)

题目链接:POJ 3190

Describe:
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

题目大意:

有n头奶牛要挤奶,给出每头牛的挤奶时间段,问在不冲突的情况下,最少需要多少个挤奶槽,并且输出每头牛所用挤奶槽的编号。

解题思路:

按开始时间升序排列,每次取出一头牛,如果有奶槽空闲,就安排到那个槽里,否则增加一个槽,同时记录每头牛所用奶槽的编号。

AC代码:

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 using namespace std;
 5 struct node // 存储奶牛,id为编号,st,ed为开始结束时间
 6 {
 7     int id,st,ed;
 8     bool operator < (const node &tmp) const // 按开始升序排列
 9     {
10         if(st == tmp.st) return ed > tmp.ed;
11         else return st > tmp.st;
12     }
13 };
14 struct mach // 存储奶槽信息
15 {
16     int id,e; // id为奶槽编号,e为当前奶槽结束时间
17     bool operator < (const mach &tmp) const // 按结束时间升序排列,为要得到最小结束时间
18     {
19         return e > tmp.e;
20     }
21 };
22 const int N = 1000010;
23 int n,x,y,sum,a[N]; // n,x,y为输入变量,sum为奶槽总数,a[N]存储i头牛所用奶槽编号
24 inline int read() // 快读
25 {
26     int x = 0, y = 1; char c = getchar();
27     while(c < '0' || c > '9') {if(c == '-') y = -1; c = getchar();}
28     while(c >= '0' && c <= '9') x = x*10+c-'0',c = getchar();
29     return x*y;
30 }
31 int main()
32 {
33     priority_queue<node> q; // 存储奶牛
34     priority_queue<mach> p; // 存储奶槽
35     n = read();
36     sum = 1; // 初始化为一个奶槽
37     for(int i = 1; i <= n; i++)
38     {
39         x = read();
40         y = read();
41         node tmp;
42         tmp.st = x;
43         tmp.ed = y;
44         tmp.id = i;
45         q.push(tmp);
46     }
47     while(!q.empty())
48     {
49         node tmp = q.top(); q.pop(); // 每次取出一头牛
50         if(p.empty()) // 如果无奶槽被占用,直接把牛放进去
51         {
52             p.push({sum,tmp.ed});
53             a[tmp.id] = sum; // 记录奶槽编号
54         } else if(p.top().e < tmp.st) { // 如果有个奶槽可以用
55             mach t = p.top();
56             p.pop();
57             p.push({t.id,tmp.ed});
58             a[tmp.id] = t.id;
59         } else if(p.top().e >= tmp.st) { // 如果没有奶槽可以用
60             sum++;
61             p.push({sum,tmp.ed});
62             a[tmp.id] = sum;
63         }
64     }
65     printf("%d\n",sum);
66     for(int i = 1; i <= n; i++)
67     {
68         printf("%d",a[i]);
69         if(i != n) printf("\n");
70     }
71     return 0;
72 }

 

posted @ 2020-09-22 14:33  不敢说的梦  阅读(163)  评论(0)    收藏  举报