最小化价格

题解:贪心,先处理人数多的组,把符合条件(能装下整个队伍的地点)的丢到优先队列里(保证没有遗漏可能的解)。然后遍历每个组时取队列里的最小值就可以了。

 
 1 #pragma warning(disable:4996)
 2 #include<queue>
 3 #include<vector>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 #define ll long long 
 9 using namespace std;
10 typedef pair<int, int> P;
11 
12 const int maxn = 100005;
13 
14 priority_queue< int, vector<int>, greater<int> >q;
15 
16 int n, m;
17 int a[maxn];
18 
19 bool cmp_1(int aa, int bb) {
20     return aa > bb;
21 }
22 
23 bool cmp_2(P aa, P bb) {
24     return aa.first > bb.first;
25 }
26 
27 int main()
28 {
29     while (cin >> n >> m) {
30         P p[maxn];
31         for (int i = 1; i <= n; i++) cin >> a[i];
32         for (int i = 1; i <= m; i++) {
33             int u, v;
34             cin >> u >> v;
35             p[i] = P(u, v);
36         }
37 
38         sort(a + 1, a + n + 1, cmp_1);
39         sort(p + 1, p + m + 1, cmp_2);
40 
41         int ans = 0;
42         bool flag = true;
43         for (int i = 1, j = 1; i <= n; i++) {
44             while (p[j].first >= a[i] && j <= m) {
45                 q.push(p[j].second);
46                 j++;
47             }
48             if (q.empty()) {
49                 flag = false;
50                 break;
51             }
52             ans += q.top();
53             q.pop();
54         }
55 
56         if (!flag) cout << "-1" << endl;
57         else cout << ans << endl;
58     }
59     return 0;
60 }

 

posted @ 2018-05-12 23:32  天之道,利而不害  阅读(225)  评论(0编辑  收藏  举报