其他分享
首页 > 其他分享> > 畜栏预定

畜栏预定

作者:互联网

 

 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 }

 

标签:int,second,stall,预定,heap,cows,畜栏,first
来源: https://www.cnblogs.com/fx1998/p/13977825.html