PAT-A的最后一题,最终做出来了...

是贪心,通过局部最优获得全局最优。

1. 将加油站按距离升序排序

2. 记录当前所在的加油站index,存有的汽油,花费。向后遍历全部 该站可抵达的加油站

3. 遍历中有两种情况:

1) 若发现油价比index更低的站next:

立即跳到该站(此时可能须要加油),不再继续遍历 —— 由于即使想要到达next后面的站,能够通过在next站购买更廉价的汽油来实现

2) 没有发现油价比index更低的站,则选择全部站中油价最低的站作为next:

此时考虑能否通过index抵达终点,若能,直接break, 不用管next;  若不能,则装满油,并行驶到next站.

4. 測试点2測试最大距离为0的情况 —— 即在起始点没有加油站。

代码:

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm> using namespace std; struct Station
{
double price;
int dis;
Station(double p, int d): price(p), dis(d) {}
friend bool operator <(const Station& a, const Station& b)
{
return a.dis < b.dis;
}
}; int main()
{
vector<Station> station;
double cmax, dest, davg, p;
int n, d;
int index = 0; // index indicate the station where they are.
double cost = 0, gas = 0;
cin >> cmax >> dest >> davg >> n;
for (int i = 0; i < n; ++ i)
{
cin >> p >> d;
station.push_back( Station(p, d) );
}
sort(station.begin(), station.end());
if (station[0].dis != 0)
{
cout << "The maximum travel distance = 0.00" << endl;
return 0;
} while ( true )
{
// 选择下一个站
double min_price = 2100000000;
int next = -1;
bool find_cheaper = false;
for (int i = index+1;
i<n && station[i].dis<dest && station[i].dis<=station[index].dis+cmax*davg;
++ i)
{
if (station[i].price <= station[index].price)
{
find_cheaper = true;
next = i;
break;
} else if (station[i].price < min_price)
{
min_price = station[i].price;
next = i;
}
}
// 选择加油方式
if (find_cheaper == true)
{
if (station[next].dis - station[index].dis > gas*davg) // 油无法到达该站
{
cost += ((station[next].dis-station[index].dis)/davg - gas) * station[index].price;
gas = 0;
} else
{
gas -= (station[next].dis-station[index].dis)/davg;
}
index = next;
} else if (next != -1) // 至少能够抵达下一个更贵的站
{
if (station[index].dis + cmax*davg >= dest)
{
break; // 跳出while循环
}
cost = cost + (cmax - gas) * station[index].price; // 装满油
gas = cmax - (station[next].dis - station[index].dis) / davg;
index = next;
} else
{
break;
}
} if (station[index].dis + cmax*davg >= dest)
{
cost = cost + ((dest-station[index].dis)/davg-gas)*station[index].price;
cout << setiosflags(ios::fixed) << setprecision(2) << cost;
} else
{
cout << "The maximum travel distance = " << setiosflags(ios::fixed) << setprecision(2) << station[index].dis + cmax*davg;
} return 0;
}

PAT 1033. To Fill or Not to Fill (贪心)的更多相关文章

  1. PAT甲级1033. To Fill or Not to Fill

    PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...

  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, Gu ...

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

  4. PAT 甲级 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 oth ...

  5. PAT甲级——1033 To Fill or Not to Fill

    1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other city i ...

  6. 1033 To Fill or Not to Fill

    PAT A 1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other ...

  7. 1033. To Fill or Not to Fill (25)

     题目链接:http://www.patest.cn/contests/pat-a-practise/1033 题目: 1033. To Fill or Not to Fill (25) 时间限制 1 ...

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

  9. pat1033. To Fill or Not to Fill (25)

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

  10. PAT_A1033#To Fill or Not to Fill

    Source: PAT A1033 To Fill or Not to Fill (25 分) Description: With highways available, driving a car ...

随机推荐

  1. [Linux][Ubuntu18.04.1] nginx+php+MySQL环境搭建

    说在前面 今天在腾讯云的CVM服务器搭建了一下环境[主机:标准型S2,Unbuntu18.04的LST版本] 采用了nginx服务器(Nginx 静态处理性能比 Apache高3倍以上,不过apach ...

  2. 刽子手游戏(UVa489)

    题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

  3. 找到最大或最小的N个元素---heapq模块

    堆排序heapq的用法 基本用法: 复杂数据结构: # coding=utf- # example.py # Example of using heapq to find the N smallest ...

  4. 浅谈ES5和ES6继承和区别

    最近想在重新学下ES6,所以就把自己学到的,记录下加强下自己的理解 首先先简单的聊下ES5和ES6中的继承 1.在es5中的继承: function parent(a,b){ this a = a; ...

  5. ubuntu 防火墙关闭的80端口,开启方法

    #关闭防火墙 /etc/init.d/iptables stopservice iptables stop # 停止服务#查看防火墙信息/etc/init.d/iptables status #开放端 ...

  6. Python并发编程-队列

    队列 IPC = Inter-Process Communication 队列 先进先出 队列的几种方法 #put() #full() #get() #empty() #get-nowait() fr ...

  7. Chris and Magic Square CodeForces - 711B

    ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...

  8. 51Nod1140 矩阵相乘的结果

    随机化算法. A*B==C那么X*A*B==X*C 降到了n*n复杂度. 多次随机X判断即可. By:大奕哥 #include<bits/stdc++.h> using namespace ...

  9. [BOI2004]Sequence

    题面描述 给定整数数组$a_1,a_2,a_3...a_n$,求递增数组$b_1,b_2,b_3...b_n$ 使得$|a_1 - b_1| + |a_2 - b_2| + ... + |a_n - ...

  10. bzoj 3595

    Splay 每个节点维护一个区间. /************************************************************** Problem: 3595 User ...