王道机试指南--Fill or not Fill(加油站贪心问题)

本文深入解析了题目ToFillorNottoFill的解决方案,通过使用贪心算法,有效地解决了加油站选择问题。文章详细介绍了算法的实现过程,并在牛客网上成功验证了代码的正确性。

题目链接To Fill or Not to Fill
参考博客1033. To Fill or Not to Fill (25) -贪心算法
思路在上面的博客中已经讲的很清楚了,这里我主要讲上述思路代码实现了一下,已经在牛客网上ac掉了。
代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

const int MAX = 505;
/*
vector<int> s;//储存站点
void prints()
{
    for(int i=0; i<s.size(); i++)
        printf("%d ", s[i]);
    printf("\n");
    s.clear();
}
*/
struct Node
{
    double per;
    double dis;
    bool operator < (const Node &p) const
    {
        return dis < p.dis;
    }
};
Node p[MAX];

double Cmax, D, Darg;
int N;
bool isStationNear(int curp, double Dmax)//判断车程内是否有加油站
{
    int k = curp+1;
    if(p[k].dis-p[curp].dis<=Dmax) return 1;
    else                           return 0;
}

int checkStationPrice(int curp, double Dmax)//返回最近最便宜(比当前站点便宜)的站点,否则返回-1
{
    int k = curp+1;
    while(p[k].dis-p[curp].dis<=Dmax && k<=N)
    {
        if(p[k].per<p[curp].per)//<=/<
        {
            return k;
        }
        k++;
    }
    return -1;
}

int checkStationCheapest(int curp, double Dmax)//返回车程内最便宜的站点
{
    int k = curp+1;
    int curcp = k;
    k++;
    while(p[k].dis-p[curp].dis<=Dmax && k<=N)
    {
        if(p[k].per<p[curcp].per)
        {
            curcp = k;
        }
        k++;
    }
    return curcp;
}
int main()
{

    while(scanf("%lf%lf%lf%d",&Cmax,&D,&Darg,&N) != EOF)
    {
        for(int i=0; i<N; i++)
        {
            cin >> p[i].per >> p[i].dis;
        }
        sort(p, p+N);

        p[N].dis = D, p[N].per = 0;
        int curp = 0;//当前的站点
        double curv = 0, curo = 0, Dmax = Cmax*Darg*1.0;//curv为当前花费的费用,curo为当前车的油量,Dmax为最大车程
        while(curp!=N)
        {
            if(!isStationNear(curp, Dmax))//车程内无法到达下一个站点
            {
                double d = p[N].dis-p[curp].dis;
                if(d>Dmax)//车程内到达不了终点站
                {
                    printf("The maximum travel distance = %.2f\n", p[curp].dis+Dmax);
                    break;
                }
                else
                {
                    curv += p[curp].per*(d/(Darg*1.0)-curo);
                    curp = N;
                    //s.push_back(curp);
                    //printf("%.2f\n", curv);
                    break;
                }
            }
            else//车程内有加油站
            {
                int cheap = checkStationPrice(curp, Dmax);
                //printf("%d cheap\n", cheap);
                if(cheap!=-1)//车程内有更便宜的站点
                {
                    curv += ((p[cheap].dis-p[curp].dis)/Darg-curo)*p[curp].per;
                    curo = 0;
                    curp = cheap;
                    //s.push_back(curp);
                }
                else
                {
                    if((p[N].dis-p[curp].dis)>Dmax)//直接到不了终点
                    {
                        int cheapest = checkStationCheapest(curp, Dmax);
                        curv += (Cmax-curo)*p[curp].per;
                        curo = Cmax-(p[cheapest].dis-p[curp].dis)/Darg;
                        curp = cheapest;
                        //s.push_back(curp);
                    }
                    else
                    {
                        curv += ((p[N].dis-p[curp].dis)/Darg-curo)*p[curp].per;
                        curp = N;
                        //s.push_back(curp);
                        //printf("%.2f\n", curv);
                        break;
                    }
                }
            }
        }
        if(curp==N) printf("%.2f\n", curv);
        //prints();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值