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.

输入

Each input file contains one test case. For each case, the first line contains 4 positive numbers: C_{max}​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D_{avg}​ (≤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: P_{i}​, the unit gas price, and D_{i} (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.

输出

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.

样例输入 

样例输入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

样例输入2:

50 1300 12 2
7.10 0
7.00 600

样例输出 

样例输出1:

749.17

样例输出2:

The maximum travel distance = 1200.00

题意理解

给你 油箱容量 终点距离 每升油能跑的距离 n个加油站点

每个加油站的信息构成是 价格 和起点0之间的距离

问你最后能不能到达终点 如果可以那么输出到达终点的油量花费 

如果不行,那么输出最远能到达的距离是多少 也就是当前加油站加满油以后我最远能跑到哪里

注意的是如果起点没有设置加油站的话 我们默认是一开始是空油量的 所以直接输出到不了终点,并且跑的距离也是0  也就是The maximum travel distance = 0.00

就我们当前加油站而言

我们先确定贪心的区间 即我们从当前加油站能走到多远呢

\left [ Pos,Pos+Cmax*Davg \right ]

pos是我们当前的位置 pos+满油的量*每升油跑的距离 也就是这是我能跑的最远距离了

贪心的区间已经确定了 那么我们现在来处理贪心的具体实现步骤

  1. 在我们能够到的的区域里面我们要找到第一个低于当前起点加油站油价的加油站
  2. 如果没有找到这个加油站 我们退而求其次 找到能够到达的区域范围内相对最小价格的加油站
  3. 如果上述两者都没找到,那么表示这个范围里面没有加油站
  4. 在找到目标加油站的前提下
  • 如果目标加油站的加油钱数小于当前加油站的价格 那么我们只要加油加到刚刚好能够到达目标加油站即可 因为在目标加油站加油更加的划算 

  • 如果目标加油站的加油钱数大于当前加油站的价格 那么我们明显是在当前加油站加油更加的的划算,那么我们算一下加满还剩多少 我们全部加满

只要我们最后判断到达的点有没有到达虚拟的终点id也就是n,那么说明我们能跑到那个位置,我们输出花费即可 反之即跑不到 那么我们输出最远距离即可 注意跑不到终点的话 最后一段我们是要把油箱加满的 这样才能保证最远距离

代码 

#include<bits/stdc++.h> 
using namespace std;
typedef long long LL;
typedef unsigned long long ULL ;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
#define fx first
#define fy second
#define io ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
const int INF=0x3f3f3f3f;
const double DINF=0x7f7f7f7f;
const LL  LINF=1e18;
const int N=2e6+10;
const int M=2e2+10;
const int MOD=1e9+7;
double C_max,END,Davg;//油箱容量 终点距离 每升油能跑的距离
int n;
typedef struct oil{
    double price,dist;
}oil;
oil st[N];
// 查找加油站的位置
// 车当前位置 车当前位置+满油*每升油能跑的距离
// [pos,pos+Cmax*Davg]
bool cmp(oil a,oil b){
    if(a.dist==b.dist){
         return a.price<b.price;
    }
    else return a.dist<b.dist;
}
//看两个加油站能不能达
bool check(oil b,oil a){
    double dis=b.dist-a.dist;
    if(dis<=C_max*Davg)return true;
    else return false;
}
int main(){
    cin>>C_max>>END>>Davg>>n;
    for(int i=0;i<n;i++){
        cin>>st[i].price>>st[i].dist;
    }
    
	sort(st,st+n,cmp);
	
	st[n]={0,END};
	//起点没有加油站
	if(st[0].dist!=0){
	    printf("The maximum travel distance = 0.00\n");
	    return 0;
	}
	//走到了哪个站点
	int oil_id=0;
	//总共用了多少油钱 当前油量剩余
	double sum=0,oil_re=0;
	
	while(oil_id<n){
	    //找到在[pos,pos+Cmax*Davg]中最低油价的加油站编号    
	    int id=-1;
	    double min_price=DINF;
	    //从当前加油站开始找 
	    //我能到达的的范围内第一个低于当前油价的加油站
	    //没找到就找这个范围里面相对来说价格最小的加油站
	    for(int i=oil_id+1;i<=n&&check(st[i],st[oil_id]);i++){
	        //如果能到达的范围内的加油站小于
	        if(st[i].price<min_price){
	            min_price=st[i].price;
	            id=i;
	            //如果此时找到了低于当前加油站油价的加油站
	            if(min_price<st[oil_id].price)break;
	        }
	    }
	    //范围内没有加油站
	    if(id==-1)break;
	    //此时的要跑到目标加油站所需要的花费
	    double ne=(st[id].dist-st[oil_id].dist)/Davg;
	    //如果目标加油站的加油钱数小于当前加油站的价格
	    if(min_price<st[oil_id].price){
	        //剩余油量不够过去到目标加油站
	        if(ne>oil_re){
	            //在当前位置加刚刚好到达目标的油量
	            sum+=(ne-oil_re)*st[oil_id].price;
	            oil_re=0;//到了以后没油了
	        }
	        //剩余油量足够过去到目标加油站
	        else {
	            oil_re-=ne;
	        }
	    }
	    //目标加油站加油价格高于当前加油站的价格
	    //油箱在当前加油站直接拉满
	    else {
	        sum+=(C_max-oil_re)*st[oil_id].price;
	        oil_re=C_max-ne;
	    }
	    oil_id=id;
	}
	
	if(oil_id==n)printf("%.2lf\n",sum);//能到达终点
	//不能到输出最远能跑多远 在油箱加满以后
	else printf("The maximum travel distance = %.2f\n",st[oil_id].dist+C_max*Davg);
	
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值