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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值