1. 程式人生 > >【PAT】1033. To Fill or Not to Fill (25)【貪心演算法】

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

翻譯:由於高速公路的原因,我們可以很方便地開車從杭州到任何其他城市。但是由於車子的油箱容量限制,我們有時需要尋找路上的加油站。不同的加油站有著不同的價格。你需要謹慎選擇最便宜的路線。

INPUT FORMAT

Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=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: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,…N. All the numbers in a line are separated by a space.

翻譯:每個輸入檔案包含一組測試資料。對於每組輸入資料,第一行包括4個正數:Cmax (<= 100),油箱的最大容量;D (<=30000),杭州到目的地的距離;Davg (<=20),平均每單位油汽車可以行進的距離;N (<= 500),加油站的總數。接下來的N行每行包括一對非負數:Pi,每單位油價,Di (<=D), 車站和杭州之間的距離,從1-N。所有一行內數字用空格隔開。

OUTPUT FORMAT

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.

翻譯:對於每組輸入資料,輸出最便宜的價格,保留兩位小數。假設開始時油箱為空。如果無法到達終點,輸出 “The maximum travel distance = X” ,X為汽車最遠可以達到的距離,保留兩位小數。

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

解題思路

用貪心演算法解決,假設油箱中的油為分層的,最便宜的油在最上面,如果裝滿油可以到達下一個加油站的話,先用油箱中最便宜的油,在到達下一個加油站後用該加油站的油將油箱裝滿,並將沒用的比該加油站貴的油替換為該加油站的油(即之前沒有加)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<deque>
#include<algorithm>
#define INF 99999999
using namespace std;
typedef pair<double,double> P;
vector<P> st;
deque<P> price;
double Cmax,D,V;
int N;
int main(){
    scanf("%lf%lf%lf%d",&Cmax,&D,&V,&N);
    double p,d;
    for(int i=0;i<N;i++){
        scanf("%lf%lf",&p,&d);
        st.push_back(P(d,p));
    }
    st.push_back(P(D,0));
    sort(st.begin(),st.end()-1);
    double tour=0,money=0;
    for(int i=0;i<st.size();i++){
        if(i==0){
            if(st[i].first!=0)break;
            else{
                price.push_back(P(st[i].second,Cmax));
                continue;
            }
        }
        double C=0,Tempcount=0;
        if(tour+Cmax*V>=st[i].first){
            C=(st[i].first-tour)/V;
            Tempcount=C;
            tour=st[i].first;
        }else{
            tour=tour+Cmax*V;
            break;
        }
        while(C!=0){
            P temp=price.front();price.pop_front();
            if(temp.second>C){
                money+=temp.first*C;
                price.push_front(P(temp.first,temp.second-C));
                C=0;
            }else{
                money+=temp.first*temp.second;
                C-=temp.second;
            }
        }
        while(!price.empty()&&price.back().first>=st[i].second)Tempcount+=price.back().second,price.pop_back();
        price.push_back(P(st[i].second,Tempcount));
    }
    if(tour<D)printf("The maximum travel distance = %.2lf\n",tour);
    else printf("%.2lf\n",money);
    return 0;
}