【PAT】1033. To Fill or Not to Fill (25)【贪心算法】

题目描述

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 FORMAT

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.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括4个正数:Cmax (<= 100),油箱的最大容量;D (<=30000),杭州到目的地的距离;Davg (<=20),平均每单位油汽车可以行进的距离;N (<= 500),加油站的总数。接下来的N行每行包括一对非负数:Pi,每单位油价,Di (<=D), 车站和杭州之间的距离,从1-N。所有一行内数字用空格隔开。

OUTPUT FORMAT

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.

翻译:对于每组输入数据,输出最便宜的价格,保留两位小数。假设开始时油箱为空。如果无法到达终点,输出 “The maximum travel distance = X” ,X为汽车最远可以达到的距离,保留两位小数。


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


解题思路

用贪心算法解决,假设油箱中的油为分层的,最便宜的油在最上面,如果装满油可以到达下一个加油站的话,先用油箱中最便宜的油,在到达下一个加油站后用该加油站的油将油箱装满,并将没用的比该加油站贵的油替换为该加油站的油(即之前没有加)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<deque>
#include<algorithm>
#define INF 99999999
using namespace std;
typedef pair<double,double> P;
vector<P> st;
deque<P> price;
double Cmax,D,V;
int N;
int main(){
    scanf("%lf%lf%lf%d",&Cmax,&D,&V,&N);
    double p,d;
    for(int i=0;i<N;i++){
        scanf("%lf%lf",&p,&d);
        st.push_back(P(d,p));
    }
    st.push_back(P(D,0));
    sort(st.begin(),st.end()-1);
    double tour=0,money=0;
    for(int i=0;i<st.size();i++){
        if(i==0){
            if(st[i].first!=0)break;
            else{
                price.push_back(P(st[i].second,Cmax));
                continue;
            }
        }
        double C=0,Tempcount=0;
        if(tour+Cmax*V>=st[i].first){
            C=(st[i].first-tour)/V;
            Tempcount=C;
            tour=st[i].first;
        }else{
            tour=tour+Cmax*V;
            break;
        }
        while(C!=0){
            P temp=price.front();price.pop_front();
            if(temp.second>C){
                money+=temp.first*C;
                price.push_front(P(temp.first,temp.second-C));
                C=0;
            }else{
                money+=temp.first*temp.second;
                C-=temp.second;
            }
        }
        while(!price.empty()&&price.back().first>=st[i].second)Tempcount+=price.back().second,price.pop_back();
        price.push_back(P(st[i].second,Tempcount));
    }
    if(tour<D)printf("The maximum travel distance = %.2lf\n",tour);
    else printf("%.2lf\n",money);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值