PAT1033 To Fill or Not to Fill(贪心)

本文介绍了一个基于贪心算法解决寻找从杭州到目的地最经济驾驶路线的问题。通过分析油箱容量、每升油行驶距离及沿途各加油站的价格和位置,设计了一种策略来决定何时何地加油,以确保最低的总费用。

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

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

 题意:有一辆从杭州开往某地的车。现给定距离,油箱的容量,每升油能开的距离,以及在这一路上的加油站个数n。

接下来n行给定油价及距离杭州的距离。

每个加油站都可以加任意容量的油(最多加满)每个油站有自己的油价,问你能否顺利开到终点。如果能,求出最少花的钱。

解法:贪心

显然这是一个贪心题了,对于每个加油站,我们需要作出决策为加还是不加,如果加的话加多少。

策略是这样的:在最远能开到的加油站的这所有的几个加油站间,看是否有比当前加油站便宜的。

如果有,找到最近的那个开过去,并且油量只需要够能开到那里就可以(能有便宜的油我一分钱都不想多花)

当然可能本来油量就够也有可能,这种情况下就不用加油。

如果可见范围内没有比自己更便宜的,那么:加满油,并且开到离自己最近的那个加油站再进行下一次策略选择。

一个小技巧:将终点看作最后一个油站,且价格为0,距离为s

如果半途中发现开不到下一个加油站了,则输出不可能,同时输出最远的距离。

如果一开始没有距离为0的油站,也输出不可能。

#include <bits/stdc++.h>
#define pii pair<int, int>
#define ll long long
#define eps 1e-5
using namespace std;
const int maxn = 2e3 + 10;
struct sta{
    double p;
    int dis;
}a[maxn];
bool cmp(const sta& a, const sta& b){
    return a.dis < b.dis;
}
int main()
{
    //    freopen("/Users/vector/Desktop/testdata.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int c, d, D, n;
    cin >> c >> D >> d >> n;
    for(int i = 0; i < n; i++)
        cin >> a[i].p >> a[i].dis;
    a[n].p = 0;
    a[n].dis = D;
    sort(a, a + n, cmp);
    if(a[0].dis > 0)
    {
        cout << "The maximum travel distance = 0.00" << endl;
        return 0;
    }
    double sum = 0, oil = 0;
    int nowpos = 0;
    for(int i = 0; i <= n; i++)
    {
        int j = i, f = 0;
        if(i < n)
            if(a[i + 1].dis - a[i].dis > c * d)
            {
                cout << "The maximum travel distance = " << fixed << setprecision(2) << 1.0 * a[i].dis + c * d <<  endl;
                return 0;
            }
        while(a[++j].dis - a[i].dis < c * d)
        {
            if(j > n) break;
            if(a[j].p < a[i].p)
            {
                if(oil * d >= a[j].dis - a[i].dis)
                {
                    i = j - 1;
                    oil -= (a[j].dis - a[i].dis) / d;
                }
                else
                {
                    sum += ((a[j].dis - a[i].dis) / (1.0 * d) - oil) * a[i].p;
                    oil = 0;
                    i = j - 1;
                }
                f = 1;break;
            }
        }
        if(f) continue;
        j = i + 1;
        int loc = j;
        double mmin = a[i + 1].p;
        while(a[j].dis - a[i].dis < c * d)
        {
            if(a[j].p < mmin)
            {
                loc = j;
                mmin = a[j].p;
            }
            j++;
            if(j > n) break;
        }
        if(a[n].dis - a[i].dis > c * d)
            sum += (c - oil) * a[i].p;
        else
            sum += 1.0 * (a[n].dis - a[i].dis) / d * a[i].p;
        oil = c - 1.0 * (a[loc].dis - a[i].dis) / d;
        i = loc - 1;
        continue;
    }
    cout << fixed << setprecision(2) << sum << endl;
    return 0;
}

 

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值