zoukankan      html  css  js  c++  java
  • POJ 2431 Expedition (priority_queue或者multiset可解)

    Expedition
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 18655   Accepted: 5405

    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
    

    Hint

    INPUT DETAILS: 

    The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively. 

    OUTPUT DETAILS: 

    Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.
     
     
     
     
     
     
    题意:我现在开着汽车,车上有p升油,离目的地有l公里,每升油能跑1公里。
        中途会有n个加油站,第i个加油站距目的地a[i]公里,可以加b[i]升油。
        问:能否到达终点,如果能到输出最少加油次数。
     
     
    解析:我们可以每次都把当前的油跑完,然后看经过了哪些加油站,找一个能加最多的加油(假设我当时就加过油),
        然后继续跑,记录加油的次数即可。
     
        可以用multiset或者优先队列解这道题,即存走过的加油点。
     
    代码:
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <vector>
    #include <set>
    using namespace std;
    typedef long long ll;
    #define INF 2147483647
    
    struct node{
        
        int a;int b;
    }s[10010];
    
    multiset <int> t;
    multiset <int>::iterator it;
    
    bool cmp(node x,node y){
        return x.a < y.a;
    } 
    
    int main(){
        int n,l,p;
        cin >> n;
        for(int i = 0;i < n; i++){
            cin >> s[i].a >> s[i].b;
        }
        cin >> l >> p;
        for(int i = 0;i < n; i++){
            s[i].a = l-s[i].a;
        }
        sort(s,s+n,cmp);
        
        int ans = 0;
        
        int con = p;
        int k = 0;
        while(con < l){
            for(;s[k].a <= con; k++){
                int e = s[k].b;
                t.insert(e);
            }
            if(t.size() < 1){
                cout << -1 << endl;
                return 0;
            }
            it = t.end();it--;
            con += *it;
            ans ++;
            t.erase(it);
        }
        cout << ans << endl;
        return 0;
    } 
  • 相关阅读:
    HTML中的文本标签
    Java 数组的创建
    JavaScript实现LUHN算法验证银行卡号有效性
    JavaScript实现HTML页面集成QQ空间分享功能
    JavaScript中的三种弹出框的区别与使用
    Maven 项目中的 pom.xml 文件内容说明
    FTPClient 中 FTPClient.changeWorkingDirectory(filePath) 代码一直返回 false
    Eclipse 中 Debug 时鼠标悬停无法查看变量值
    Innodb ,MyISAM
    tomcat jetty
  • 原文地址:https://www.cnblogs.com/zhangjiuding/p/7736064.html
Copyright © 2011-2022 走看看