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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章