poj2431 Expedition

思路:

贪心 + 优先队列。和http://www.cnblogs.com/wangyiming/p/8744388.html这个题是一样的。

实现:

 1 #include <iostream>
 2 #include <queue>
 3 #include <algorithm>
 4 using namespace std;
 5 struct node
 6 {
 7     int d, f;
 8 };
 9 node a[10005];
10 bool cmp(node a, node b)
11 {
12     return a.d > b.d;
13 }
14 int main()
15 {
16     int n, l, p;
17     cin >> n;
18     for (int i = 0; i < n; i++) cin >> a[n - 1 - i].d >> a[n - 1 - i].f;
19     sort(a, a + n, cmp);
20     cin >> l >> p;
21     for (int i = 0; i < n; i++) a[i].d = l - a[i].d;
22     priority_queue<int> q;
23     int i = 0, rem = p, cnt = 0;
24     for ( ; i < n; i++) 
25     { 
26         if (a[i].d > rem) break; 
27         q.push(a[i].f); 
28     }
29     while (!q.empty())
30     {
31         if (rem >= l) break;
32         rem += q.top(); q.pop();
33         cnt++;
34         for ( ; i < n; i++) 
35         { 
36             if (a[i].d > rem) break; 
37             q.push(a[i].f); 
38         }
39     }
40     cout << (rem >= l ? cnt : -1) << endl;
41     return 0;
42 }

猜你喜欢

转载自www.cnblogs.com/wangyiming/p/9191098.html