zoukankan      html  css  js  c++  java
  • poj 2431 Expedition

    题意:

    驾驶一辆卡车形式len的距离,卡车的油箱容量无限,一开始有p单位油,一单位油行驶1单位距离。在len的距离上分布加油站,经过加油站就可以加油。

    为行驶完len的距离最少需要加多少次油或者无法到达。

    思路:

    “在到达加油站之后,就获得了在这个加油站之后任意位置加一次油的机会” 关键。

    贪心,在油用完之后才考虑加油,并且要加最多的油,那么就用到了优先队列。

    坑:

    在n个加油站跑完之后,还要判断是否已经到达终点,若没有,继续操作。。。

    代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <queue>
     4 #include <iostream>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 priority_queue<int> pq;
     9 
    10 struct node
    11 {
    12     int d,p;
    13 } a[10005];
    14 
    15 bool cmp(node aa,node bb)
    16 {
    17     if (aa.d == bb.d) return aa.p > bb.p;
    18     return aa.d < bb.d;
    19 }
    20 
    21 int main()
    22 {
    23     int n;
    24 
    25     while (scanf("%d",&n) != EOF)
    26     {
    27         while (!pq.empty()) pq.pop();
    28 
    29         for (int i = 0;i < n;i++)
    30         {
    31             scanf("%d%d",&a[i].d,&a[i].p);
    32         }
    33 
    34         int len,op;
    35 
    36         scanf("%d%d",&len,&op);
    37 
    38         for (int i = 0;i < n;i++)
    39         {
    40             a[i].d = len - a[i].d;
    41         }
    42 
    43         sort(a,a+n,cmp);
    44 
    45         int ans = 0;
    46         int cur = op;
    47 
    48         for (int i = 0;i < n;i++)
    49         {
    50             //cout << cur << "  233" << endl;
    51 
    52             if (cur >= a[i].d) pq.push(a[i].p);
    53             else
    54             {
    55                 while (!pq.empty() && cur < a[i].d)
    56                 {
    57                     ans++;
    58 
    59                     int tmp = pq.top();
    60 
    61                     pq.pop();
    62 
    63                     cur += tmp;
    64                 }
    65 
    66                 if (cur < a[i].d) break;
    67                 else pq.push(a[i].p);
    68             }
    69         }
    70 
    71         while (!pq.empty() && cur < len)
    72         {
    73             ans++;
    74 
    75             int tmp = pq.top();
    76 
    77             pq.pop();
    78 
    79             cur += tmp;
    80         }
    81 
    82         if (cur >= len) printf("%d
    ",ans);
    83         else printf("-1
    ");
    84     }
    85 
    86     return 0;
    87 }
  • 相关阅读:
    osg::BlendFunc来设置透明度
    LCA(Tarjan)
    CODEVS1073 家族 (并查集)
    CODEVS1533 互斥的数(哈希表)
    2014-12-4
    BZOJ2661 连连看 (费用流)
    2014-11-30
    JAVA语法基础作业——动手动脑以及课后实验性问题
    课后作业01——相加
    再读大道至简第二章
  • 原文地址:https://www.cnblogs.com/kickit/p/8029133.html
Copyright © 2011-2022 走看看