详解PAT甲级1033 贪心算法

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 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

解法:

既然是贪心算法,那么就要问了

贪什么?

贪油价(让每个单位油价达到最便宜),则最终油价就是最便宜。

怎么贪?

每个单位距离(单位距离是指每升油所走的那段距离为一个单位)的油价是什么决定的?是此位置之前的加油站的价格,那么每个点之前有那么多加油站要用哪个加油站加油呢?当然是最便宜的加油站咯,也就是说贪心策略就是遍历每个单位距离的时候,都去选择这个单位距离的油所用的最便宜的加油站

先将加油站按距离顺序排好

前提:

1.假设每个加油站都加满油,也就是说每个加油站之后都有一个最大行驶距离,暂时叫它为某个加油站的作用范围

2.如上图,蓝线内的每升油所行驶的单位距离,可以来源于加油站1,2,3,姑且把这些单位可选择的加油站所组成的集合叫做这个单位的可选择集合

3.集合中的所有加油站的作用范围会有一个交集(如图中1,2,3的交集就是[3,4]之间),在此交集下的每个单位所能选择的加油站是相同的,也就是说这些集合下的单位总共选一次就可以了,又由于

(1)每次碰见加油站时都要将加油站更新进集合

(2)当某个加油站的作用范围失去(就是出去该单位点后方所有加油站作用范围的交集,就比如在上图集合1,2,3中出来,到达第4个加油站再往前一点,就是破坏了这个所有加油站作用范围的交集,在4这个加油站后的单位的可选择集合都失去了1加油站),这一个加油站从集合中删去

所以我们要维护这个交集,怎么维护呢?

做法:

先称两个相邻加油站之间的距离叫做邻站距离

a.按照邻站距离为一个单位去选择加油站,在到达下一个加油站之前去选择加油站,然后再将这个位置的加油站更新到集合中,选择邻站距离中的所有单位要用最便宜油的加油站,这个就解决了(1)

b.在加入加油站后再去观察下一段邻站距离中,会不会出现在邻站距离间有一个加油站失去作用范围的情况,当然这个肯定是最前面的加油站,所以一开始要在最前面的加油站上放个哨兵j,然后在这个集合中删去这个站点,更新哨兵j去下一个最前面的加油站,这个就解决了(2)

就如下图在2,3,4的交集中4-5加油站之间

利用map容器自动升序的性质,代码如下:

#include<bits/stdc++.h>
using namespace std;
struct station{
    double pi,di;

};
double Cmax,D,Davg;
//总油量,总距离,公里行走距离
int N;
//加油站数量
station s[501];
bool comp(station x,station y){
    return x.di<y.di;
}

int main(){
    cin>>Cmax>>D>>Davg>>N;
    for(int i=0;i<N;i++) cin>>s[i].pi>>s[i].di;
    s[N].di = D;
    sort(s,s+N+1,comp);

    double curdis=0,curcost=0;
    map<double,int> M;
    for(int i=0,j=0;s[i].di<D;i++){
        if(curdis<s[i].di){
            if(!M.size()) break;
            curcost+=M.begin()->first*(s[i].di-curdis)/Davg;
            curdis = s[i].di;
        }//来到i站
        M[s[i].pi]++;
        
        while(j<=i&&s[j].di+Cmax*Davg<s[i+1].di){
            curcost+=M.begin()->first*(s[j].di+Cmax*Davg-curdis)/Davg;
            curdis = s[j].di+Cmax*Davg;
            M[s[j].pi]--;
            if(!M[s[j].pi])M.erase(s[j].pi);
            j++;
        }
    }
    if(!M.size()){
        printf("The maximum travel distance = %.2f",curdis);
    }else{
        curcost+=M.begin()->first*(D-curdis)/Davg;
        curdis = D;
        printf("%.2f",curcost);
    }
}

  • 18
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值