poj2431 Expedition

本文介绍了一个结合贪心算法与优先队列解决特定问题的实例。通过对输入数据进行排序并使用优先队列来优化资源分配过程,实现了有效的解决方案。文章提供了完整的C++代码实现,有助于理解算法细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:

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

 

转载于:https://www.cnblogs.com/wangyiming/p/9191098.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值