文章

34

粉丝

0

获赞

6

访问

1.0k

头像
To Fill or Not to Fi 题解(贪心):
P1347 浙江大学机试题
发布于2025年8月7日 16:28
阅读数 26

贪心策略:

  • 在当前加油站,寻找在最大行驶距离(即满油箱所能行驶的距离)内价格最低的加油站作为下一个目的地。

  • 距离从近到远找,如果找到了加油站价格比当前加油站更低,无需判断后续加油站,只加足够到达该加油站的油;否则加满油。

 

#include<bits/stdc++.h>
using namespace std;

const int MAXN = 505;
const double INF = 1e9;

struct Station {
    double price;
    double dis;
    bool operator<(const Station& other) const {
        return dis < other.dis;
    }
};

Station stations[MAXN];

int main() {
    double Cmax, D, Davg;
    int N;
    while (cin >> Cmax >> D >> Davg >> N) {
        for (int i = 0; i < N; ++i) {
            cin >> stations[i].price >> stations[i].dis;
        }
        stations[N].price = 0.0;
        stations[N].dis = D;
        sort(stations, stations + N + 1);

        if (stations[0].dis != 0) {
            printf("The maximum travel distance = 0.00\n");
            continue;
        }

        double maxDis = Cmax * Davg;
        double curDis = 0.0;
 ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发