题目1437:To Fill or Not to Fill

时间限制:1 秒

内存限制:128 兆

(贪心有点复杂)

题目描述:

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.

输入:

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.

输出:

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.

样例输入:
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
50 1300 12 2
7.10 0
7.00 600
样例输出:
749.17
The maximum travel distance = 1200.00
#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;

class Station
{
public:
    int dis;
    float pri;
    Station()
    {
        dis = 0;
        pri = 0;
    }

};

bool cmp(Station a, Station b)
{
    return a.dis < b.dis;
}

Station s[501];
float res;//结果

int main()
{

    int cmax, d,  n, i, j;
    float davg;
    float maxDis;//加满一次行驶最大距离
    float res = 0;

    while(cin >> cmax)
    {
        res = 0;
        cin >> d >> davg >> n;
        maxDis = cmax * davg;
        for (i=0; i<n; i++)
        {
            cin >> s[i].pri >> s[i].dis;
        }
        s[n].dis = d;//去特殊化
        sort(s, s+n, cmp);
        if (s[0].dis != 0)
        {
            cout << "The maximum travel distance = 0.00" << endl;
            continue;
        }

        int curPos=0;
        float curOil=0;//当前距离、油箱剩油
        for (i=0; i<n ; i++)//每次一站
        {
            curPos = s[i].dis;
            //无法到达终点
            if (curPos + maxDis < s[i+1].dis)
            {
                cout << "The maximum travel distance = ";
                cout << setiosflags(ios::fixed) << setprecision(2) << curPos+maxDis +0.0<< endl;
                break;
            }
            //减掉中间行驶耗油
            if (i>0)
            {
                curOil = curOil - (s[i].dis - s[i-1].dis)/davg;
            }
            //如果最大距离内有较便宜
            int curDis = 0;
            bool isCheap = false;
            for (j=i+1; j<n; j++)
            {
                curDis = s[j].dis - s[i].dis;
                if (curDis>maxDis)//如果没有
                {
                    //res += (cmax - curOil)*s[i].pri;
                    //curOil = cmax;
                    break;
                }
                if (s[j].pri<s[i].pri)//有行驶到便宜站
                {
                    float temp = curOil;

                    curOil = (s[j].dis - s[i].dis )/davg;//应保证油量
                    if (temp - curOil < 0)
                    {
                        res += (curOil - temp)*s[i].pri;//补充加油
                    }
                    else
                    {
                        curOil = temp;//不需要加油
                    }
                    isCheap = true;
                    break;
                }
            }// end of for
            //最大距离内未找到便宜站
            if (!isCheap)
            {
                if (curPos+maxDis>=d)//到达终点
                {
                    res += ((d - curPos)/davg - curOil) *s[i].pri;
                    cout << setiosflags(ios::fixed) << setprecision(2);
                    cout << res << endl;

                    break;
                }
                else//未到终点,加满油
                {
                    res += (cmax - curOil)*s[i].pri;
                    curOil = cmax;
                }
            }
        }//end of for (每次一站)

    }//end of while
    return 0;
}

时间: 2024-10-08 06:51:23

题目1437:To Fill or Not to Fill的相关文章

九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: 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 fro

PAT 1033. To Fill or Not to Fill (25)(贪心)

解题思路: 其实题目本身不难,难就难在对贪心策略的选取 在每一个加油点应该做这样的判断: 1.首先计算从本加油点做多可能达到的加油点 2.如果有可达加油点的话: 2.1 寻找有无比当前加油点油价便宜的加油点,如果有的话就跑到该便宜的加油点,油量加到支持到该加油点即可 2.2 如果没有比当前加油点更便宜的加油点的话,又分两种情况: 2.2.1.如果本次加油可能直接到终点,就加可以到达终点的油量 2.2.2.否则的话,加满 3.如果没有可达加油点的话: 3.1 看是否可以直接达到终点 3.2 不能达

【贪心】PAT 1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 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 fin

PAT 1033 To Fill or Not to Fill[dp]

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 gi

pat1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25) 时间限制 10 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 fin

1033 To Fill or Not to Fill (25 分)

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 g

题目1437:To Fill or Not to Fill(贪心算法)

题目描述: 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

To Fill or Not to Fill(贪心模拟)

题目描述: 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

PAT 1033. To Fill or Not to Fill

#include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> using namespace std; int main() { int N; double mcap, dist, davg; scanf("%lf%lf%lf%d", &mcap, &dist, &davg, &N); double price, id