hihocoder1705 座位问题

思路:

使用堆模拟。复习了priority_queue自定义结构体比较函数的用法。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int, int> pii;
 4 int n, m, k;
 5 
 6 struct cmp
 7 {
 8     bool operator()(const pii & a, const pii & b)
 9     {
10         if (a.first != b.first) return a.first < b.first;
11         return a.second > b.second;
12     }
13 };
14 int main()
15 {
16     cin >> n >> m >> k;
17     priority_queue<pii, vector<pii>, cmp> q;
18     vector<int> v;
19     int x;
20     for (int i = 0; i < m; i++) { cin >> x; v.push_back(x); }
21     v.push_back(0); v.push_back(n + 1);
22     sort(v.begin(), v.end());
23     for (int i = 0; i < v.size() - 1; i++) q.push(pii(v[i + 1] - v[i] - 1, v[i] + 1));
24     for (int i = 0; i < k; i++)
25     {
26         pii tmp = q.top(); q.pop();
27         int pos = tmp.second + (tmp.first - 1) / 2;
28         cout << pos << endl;
29         q.push(pii(pos - tmp.second, tmp.second));
30         int r = tmp.second + tmp.first;
31         q.push(pii(r - pos - 1, pos + 1));
32     }
33     return 0;
34 }
原文地址:https://www.cnblogs.com/wangyiming/p/8548476.html