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. hdu 3573(数学+贪心)

    Buy Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. TeX Live & TeXstudio 安装手记

    数据库课上又看到了那位用 beamer 做 slides 的师兄,想到自己一拖再拖的LaTeX入门,决定赶快动手装个环境再说~在经过一番搜索和研究之后决定先在 windows 底下试用,选择 TeX ...

  3. mysql左连接,右连接,内连接

  4. HDU 5876 Sparse Graph(补图上BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...

  5. 洛谷P2801 教主的魔法 [分块,二分答案]

    题目传送门 教主的魔法 题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. ...

  6. Django+Nginx+uwsgi搭建自己的博客(五)

    在上一篇博文中,向大家介绍了Users App和Index的前端部分的实现,以及前端与Django的通信部分.至此,我们的博客已经具备一个简单的雏形,可以将其部署在本地的服务器上了.目前较为流行的we ...

  7. python配置libsvm

    转载博文:win10(64-bit) + python3.6.0(64-bit) 配置libsvm-3.22 https://blog.csdn.net/weixin_35884839/article ...

  8. 分布式锁的理解,java自带的锁为什么会失效

    前段时间在发送短信的代码块上通过网上找的工具类基于Redis实现了分布式锁的功能 对应的链接https://www.cnblogs.com/c-h-y/p/9391602.html 周末想细细看一下. ...

  9. Typora ---一款简洁的Markdown编辑器

    Typora BB in front 如果你是一个佛(lan)系(duo),内心文艺的程序员,并且你对其他Markdown编辑器的使用效果感觉不是很好的话,可以来了解一下该软件Typora. What ...

  10. pair 对组

    pair 对组 c++ 基础 2016-05-10 19:42 154人阅读 评论(0) 收藏 举报  分类: 头文件的函数精粹(12)  版权声明:本文为博主原创文章,未经博主允许不得转载. 与关联 ...