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

    Expedition
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4023   Accepted: 1309

    Description

    A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels. 
    To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop). 
    The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000). 
    Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all. 

    Input

    * Line 1: A single integer, N 
    * Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop. 
    * Line N+2: Two space-separated integers, L and P

    Output

    * Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.

    Sample Input

    4
    4 4
    5 2
    11 5
    15 10
    25 10
    

    Sample Output

    2

    一辆车在路上行驶,路上有n个加油站,问车至少加多少次油才能走到目的地。

    模拟车行驶的过程,将每次经过的加油站维护一个大根堆,每当车上的油不够走到下一个加油站时,就将堆中能加油最大的那个加油站取出来,为车加油。

    若是所有的加油站都取出来仍无法使车开到下一个加油站或是终点,则输出-1即可

     

      1 #include<iostream>
      2 #include<cstdio>
      3 
      4 using namespace std;
      5 
      6 typedef struct
      7 {
      8     long fuel;
      9     long dis;
     10 } STOP;
     11 
     12 long n,l,p,sz,ans;
     13 STOP temp[10010],heap[10010];
     14 
     15 void push(STOP x)
     16 {
     17     long i=sz++;
     18 
     19     while(i>0)
     20     {
     21         long p=(i-1)/2;
     22 
     23         if(heap[p].fuel>=x.fuel)
     24             break;
     25         heap[i]=heap[p];
     26         i=p;
     27     }
     28 
     29     heap[i]=x;
     30 }
     31 
     32 STOP pop()
     33 {
     34     STOP ret=heap[0];
     35 
     36     STOP x=heap[--sz];
     37 
     38     long i=0;
     39 
     40     while(i*2+1<sz)
     41     {
     42         long a=i*2+1,b=i*2+2;
     43 
     44         if(b<sz&&heap[b].fuel>heap[a].fuel)
     45             a=b;
     46 
     47         if(heap[a].fuel<=x.fuel)
     48             break;
     49 
     50         heap[i]=heap[a];
     51         i=a;
     52     }
     53 
     54     heap[i]=x;
     55 
     56     return ret;
     57 }
     58 
     59 long Partition(long p,long r)
     60 {
     61     STOP x=temp[r];
     62     int i=p-1;
     63 
     64     for(long j=p;j<r;j++)
     65     {
     66         if(temp[j].dis<x.dis)
     67         {
     68             STOP tmp=temp[j];
     69             temp[j]=temp[i+1];
     70             temp[i+1]=tmp;
     71             i++;
     72         }
     73     }
     74 
     75     STOP tmp=temp[r];
     76     temp[r]=temp[i+1];
     77     temp[i+1]=tmp;
     78 
     79     return i+1;
     80 }
     81 
     82 void QuickSort(long p,long r)
     83 {
     84     if(p<r)
     85     {
     86         long q=Partition(p,r);
     87         QuickSort(p,q-1);
     88         QuickSort(q+1,r);
     89     }
     90 }
     91 
     92 int main()
     93 {
     94     while(scanf("%ld",&n)==1)
     95     {
     96         for(int i=1;i<=n;i++)
     97             scanf("%ld %ld",&temp[i].dis,&temp[i].fuel);
     98         scanf("%ld %ld",&l,&p);
     99         for(int i=1;i<=n;i++)
    100             temp[i].dis=l-temp[i].dis;
    101 
    102         sz=ans=0;
    103 
    104         QuickSort(1,n);
    105 
    106         long Final=l-temp[n].dis;
    107 
    108         for(int i=n;i>=2;i--)
    109             temp[i].dis=temp[i].dis-temp[i-1].dis;
    110 
    111         for(int i=1;i<=n;i++)
    112         {
    113             while(p<temp[i].dis&&sz>0)
    114             {
    115                 STOP t=pop();
    116                 p+=t.fuel;
    117                 ans++;
    118             }
    119 
    120             if(p<temp[i].dis)
    121             {
    122                 ans=-1;
    123                 break;
    124             }
    125             else
    126             {
    127                 p-=temp[i].dis;
    128                 push(temp[i]);
    129             }
    130         }
    131 
    132         if(ans==-1)
    133             cout<<ans<<endl;
    134         else
    135         {
    136             while(p<Final&&sz>0)
    137             {
    138                 STOP t=pop();
    139                 p+=t.fuel;
    140                 ans++;
    141             }
    142 
    143             if(p<Final)
    144                 cout<<"-1"<<endl;
    145             else
    146                 cout<<ans<<endl;
    147         }
    148     }
    149 
    150     return 0;
    151 }
    [C++]
  • 相关阅读:
    hdu 1253 胜利大逃亡 (三维简单bfs+剪枝)
    OpenCV中OpenCL模块函数
    基于年纪和成本(Age & Cost)的缓存替换(cache replacement)机制
    POJ 1637 混合图求欧拉回路 最大流实现
    Linux-shell-算术运算{expr、bc、dc、(( ))和[ ]}
    uboot环境变量与内核MTD分区关系
    nor flash 和nand flash 的区别
    NAND Flash大容量存储器K9F1G08U的坏块管理方法
    嵌入式学习之Nand Flash
    s3c2440对nandflash的操作
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3234414.html
Copyright © 2011-2022 走看看