1033. To Fill or Not to Fill

1033. To Fill or Not to Fill (25)

時間限制
100 ms
內存限制
65536 kB
代碼長度限制
16000 B
判題程序
Standard
作者
ZHANG, Guochuan

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

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

提交代

挺好的一個題,剛開始沒有思路,然後想了想以爲是區間貪心,結果還是不對,然後看了看講解。明白了具體思路,先將加油站按照座標排序,然後對於每一個座標,有兩種決策方案:1.在該點能夠達到的所有點中,尋找第一個油價低於你當前所在的油價的點,然後在油箱加夠正好到達該點的油到達該點;2.如果在該點能夠到達的所有點中,如果沒有油價低於當前油價的點,則將油箱加滿,到達距離該點最遠的點,因爲其他點油更貴,所以還不在該點加滿油。

其實想這個題的時候,我只完整的想到了第一點,至於第二點,我當初的想法是尋找這些點中價格最低的點,結果越想越麻煩,每次找最小的點都很麻煩,而且我從沒想過會讓油箱加滿油,只想讓油箱裏加入剛好走到下一個點的油。總之,這個題是我在知道思路後解決了,沒有看源碼。所以是一道很不錯的題,開拓了自己的思維。

注意兩點:1.起始點的座標也許不是0。

2.剛開始輸入的油箱容量,距離,以及單位數量的油行駛的距離都是浮點類型的。

這兩點完全沒想到,考慮問題不周密,以下是我寫的代碼:

 

#include<bits/stdc++.h>
using namespace std;
struct node{
    double p;
    double x;
}a[505];
bool cmp(node x,node y){
    return x.x<y.x;
}
int main(){

    double c,d,s;
    int n,len=0,len1=0;
    scanf("%lf%lf%lf%d",&c,&d,&s,&n);
    for(int i=1;i<=n;i++){
        scanf("%lf%lf", &a[i].p, &a[i].x);
    }
    n++;
    a[n].x=d;
    a[n].p=0;
    sort(a+1,a+1+n,cmp);
    if(a[1].x!=0){
        printf("The maximum travel distance = 0.00\n");
    }else{
        int start=1,f=1;
        double rest=0,sum=0;
        node e;
        while(1){
            if(start==n||!f) break;
            int flag=0,x;
            for(int i=start+1;i<=n;i++){
                if(a[i].p>=a[start].p&&a[i].x-a[start].x<=c*s){
                    e.p=a[i].p;
                    e.x=a[i].x;
                    x=i;
                }else if(a[i].p<a[start].p&&a[i].x-a[start].x<=c*s){
                    flag=1;
                    e.p=a[i].p;
                    e.x=a[i].x;
                    x=i;
                    break;
                }else if(a[i].x-a[start].x>c*s&&i==n){
                    f=0;
                    x=i-1;
                    break;
                }else{
                    break;
                }
            }
            if(f){
                if(flag){
                    if(rest>=(e.x-a[start].x)/s){
                        rest=rest-(e.x-a[start].x)/s;
                    }else{
                        sum=sum+((e.x-a[start].x)/s-rest)*a[start].p;
                        rest=0;
                    }
                }else{
                    sum=sum+(c-rest)*a[start].p;
                    rest=c-(e.x-a[start].x)/s;
                }
            }
            start=x;
        }
        if(f){
            printf("%.2lf\n",sum);
        }else{
            printf("The maximum travel distance = %.2lf\n",a[start].x+c*s);
        }
    }
    return 0;
}

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