1033 To Fill or Not to Fill(PAT甲)(初识贪心思想)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; Davg (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print The maximum travel distance = X where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:

50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300

Sample Output 1:

749.17

Sample Input 2:

50 1300 12 2
7.10 0
7.00 600

Sample Output 2:

The maximum travel distance = 1200.00

代码长度限制16 KB

时间限制200 ms

内存限制64 MB

//参考Reza.思路讲解http://t.csdn.cn/nLn8e
//参考柳婼代码http://t.csdn.cn/ul8RQ 
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

const int inf = 99999999;
struct station{
    double price,dis;
};
bool cmp1(station a,station b){
    return a.dis<b.dis;
}

int main(){
    double cmax,d,davg;
    int n;
    cin>>cmax>>d>>davg>>n;
    vector<station> sta(n+1);
    sta[0]={0.0,d};
    for(int i=1;i<=n;i++){
        scanf("%lf%lf",&sta[i].price,&sta[i].dis);
    }
    sort(sta.begin(),sta.end(),cmp1);//将各个站点按距离排序
    double nowdis = 0.0,
    maxdis = 0.0,nowprice = 0.0,
    totalPrice = 0.0,leftdis = 0.0;
    if(sta[0].dis!=0){//如果初始没有距离为0的加油站,既无法开出去
        cout<<"The maximum travel distance = 0.00";
        return 0;
    }else{
        nowprice = sta[0].price;
    }
    int now=0;
    while(nowdis<d){//不断循环直到到达目的地
        
        maxdis = nowdis+cmax*davg;
        
        double mp=inf;
        //vector<double> minPrice;
        //fill(minPrice,minPrice+501,inf);
        int ms;
        //vector<int> minsta;
        //int flag=0;
        int i;
        for(i=now+1;i<=n&&sta[i].dis<=maxdis;i++){//寻找当前你可达的最远范围内比你当前站点便宜的站,每次只加刚好能到其距离所需油费,
            
            if(sta[i].price>nowprice&&sta[i+1].dis>maxdis&&i<n){//若没有找一个相对便宜的
                for(int j=now+1;j<=i;j++)
                if(sta[j].price<mp){
                    mp=sta[j].price;
                    ms=j;
                }
            } 
            else if(sta[i].price<=nowprice){//找到便宜站点就更新
                totalPrice=totalPrice+nowprice*((sta[i].dis-nowdis-leftdis)/davg);
            //    cout<<" -"<<totalPrice<<"- \n";
                nowdis=sta[i].dis;
                nowprice=sta[i].price;
                now=i;
                maxdis = nowdis+cmax*davg;
                leftdis=0;
            }
            
        }
        if(i<=n&&sta[i].dis-sta[i-1].dis>cmax*davg){//若相邻站点距离过大,就输出你能到的最远距离
            printf("The maximum travel distance = %.2f", maxdis);
            return 0;
        }
        if(nowdis<d&&mp!=inf){//找到相对便宜的后,在当前站点加满,然后再到相对便宜的站点补充
            totalPrice=totalPrice+nowprice*(cmax-leftdis/davg);
        //    cout<<" -"<<totalPrice<<"- \n";
            leftdis=cmax*davg-(sta[ms].dis-nowdis);//用不完留着,下次可以少加点
            nowdis=sta[ms].dis;
            nowprice=mp;
        //    cout<<mp<<" "<<nowdis<<" ";
            now=ms;
            maxdis = nowdis+cmax*davg;
        }    
    }
    if(nowdis==d){
        printf("%.2f", totalPrice);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值