# 畜栏预定

 1 #include <bits/stdc++.h>
2 using namespace std;
3 typedef pair<int, int> PII;
4 const int N = 50010;
5 pair<PII, int> cows[N];
6 //首先存储开始时间和结束时间，然后存储编号id
7 int id[N]; //存储n头牛依次放在哪个畜栏
8 int main() {
9     int n;
10     cin >> n;
11     for (int i = 0; i < n; i++) {
12         cin >> cows[i].first.first >> cows[i].first.second;
13         cows[i].second = i;
14     }
15     sort(cows, cows + n); //从小到大排序
16     priority_queue<PII, vector<PII>, greater<PII> > heap;
17     for (int i = 0; i < n; i++) {
18         PII cow = cows[i].first;
19         if (heap.empty() || heap.top().first >= cow.first) {
20             PII stall = {cow.second, heap.size() + 1}; //新增一个畜栏
21             //first是终止时间，second是编号，注意是从1开始
22             id[cows[i].second] = stall.second;
23             heap.push(stall);
24         } else {
25             //否则，放入最早结束的那个畜栏
26             PII stall = heap.top();
27             heap.pop();
28             stall.first = cow.second;
29             id[cows[i].second] = stall.second;
30             heap.push(stall);
31         }
32     }
33     cout << heap.size() << endl;
34     for (int i = 0; i < n; i++) {
35         cout << id[i] << endl;
36     }
37     return 0;
38 }

posted @ 2020-11-15 18:53  kyk333  阅读(118)  评论(0编辑  收藏  举报