码迷,mamicode.com
首页 > 其他好文 > 详细

poj2431 Expedition

时间:2018-06-16 19:11:28      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:poj   code   blank   href   +=   queue   rem   space   blog   

思路:

贪心 + 优先队列。和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 }

 

poj2431 Expedition

标签:poj   code   blank   href   +=   queue   rem   space   blog   

原文地址:https://www.cnblogs.com/wangyiming/p/9191098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!