PAT 1033. To Fill or Not to Fill (贪婪)
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 (贪婪)的更多相关文章
- PAT甲级1033. To Fill or Not to Fill
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- 【贪心】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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Android Wear
数据类型和接口的发送和同步数据概述
Android Wear数据层API,这是google play service部分,通信信道,以你的手持设备和耐磨应用. Api它包含一系列数据对象,可以让系统通过监控和通知行app重要的事件数据层 ...
- 什么是IT
这个是同事总结的,我补充了若干项,算不上原创,但这个没有在其他地方看到,在这儿权且当原创了.后面再配个软件架构图吧.看到缺的同学能够补充 什么是IT:Information-信息Technology- ...
- POJ 1324 Holedox Moving 搜索
题目地址: http://poj.org/problem?id=1324 优先队列---A*的估价函数不能为蛇头到(1,1)的距离,这样会出错. 看了discuss,有大神说这题A*的估价函数为BFS ...
- android 编译调用C代码
博客地址:www.zalezone.cn 前言 需求来源 这几天帮别人做一个简单的androidclient,也没什么功能,主要就是调用C代码来对手机的Wifi网络进行设置.于是也就引出了技术难点所在 ...
- 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom
原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...
- lua5.1 和 5.2 关于 sequence 的定义变化,对#table取值的影响
引子 环境 lua 5.2 a = {} for i=1,2 do a[i] = i*3 end a[4] = 11; print(a[#a]) ---print 11 ------- ...
- Java反射探索研究(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankakay 摘要:本文详细深入讲解是Java中反射的机制,并介绍了如何通过反射来生成对象.调用函数.取得 ...
- iOS:编译错误 linker command failed with exit code 1 (use -v to see invocation)
将project不加入.m要求加入 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzI0MzQ2OQ==/font/5a6L5L2T/fontsi ...
- BZOJ 1052 HAOI2007 覆盖问题 二分法答案+DFS
标题效果:特定n点.涵盖所有的点与同方三面.斧头要求方垂直边界,最小平方的需求方长值 最大值至少.答案是很明显的二分法 但验证是一个问题 考虑仅仅有三个正方形,故用一个最小矩形覆盖这三个正方形时至少有 ...
- 连接字符串中Min Pool Size的理解是错误,超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
Min Pool Size的理解是错误的 假设我们在一个ASP.NET应用程序的连接字符串中将Min Pool Size设置为30: <add name="cnblogs" ...