BZOJ1747 [Usaco2005 open]Expedition 探险

首先我们可以发现如果错过了一个加油站,而继续往前走的时候没有油了,可以再假装之前经过加油站的时候加过油

于是我们维护一个大根堆,表示错过的加油站是哪些,每当没有油的时候从堆顶取出最大值加上去即可

 

 1 /**************************************************************
 2     Problem: 1747
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:20 ms
 7     Memory:916 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <algorithm>
12 #include <queue>
13  
14 using namespace std;
15 const int N = 1e4 + 5;
16  
17 inline int read();
18  
19 struct data {
20     int w, add;
21      
22     inline void get() {
23         w = read(), add = read();
24     }
25      
26     inline bool operator < (const data &d) const {
27         return w > d.w;
28     }
29 } a[N];
30  
31 int n, tot, ans;
32 priority_queue <int> h;
33  
34 int main() {
35     int i;
36     n = read();
37     for (i = 1; i <= n; ++i) a[i].get();
38     a[0].w = read(), tot = read();
39     sort(a + 1, a + n + 1);
40     a[n + 1].w = 0;
41     for (i = 1; i <= n + 1; ++i) {
42         tot -= (a[i - 1].w - a[i].w);
43         while (tot < 0 && !h.empty()) {
44             ++ans;
45             tot += h.top(), h.pop();
46         }
47         if (tot < 0) {
48             puts("-1");
49             return 0;
50         }
51         h.push(a[i].add);
52     }
53     printf("%d\n", ans);
54     return 0;
55 }
56  
57 inline int read() {
58     static int x;
59     static char ch;
60     x = 0, ch = getchar();
61     while (ch < '0' || '9' < ch)
62         ch = getchar();
63     while ('0' <= ch && ch <= '9') {
64         x = x * 10 + ch - '0';
65         ch = getchar();
66     }
67     return x;
68 }
View Code

 

转载于:https://www.cnblogs.com/rausen/p/4480575.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值