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 ...
随机推荐
- 洛谷P1554 梦中的统计 题解
题目传送门 这道题暴力又让我过了...数据真的很水(luogu) 暴力枚举n~m的每个数,再统计一次,交付评测...AC #include<bits/stdc++.h> using nam ...
- ASP.NET WebAPI 05 参数绑定
ParameterBindingAttribute 在上一篇中重点讲了ModelBinderAttribute的使用场景.这一篇详细的讲一下ModelBinder背后的参数绑定原理. ModelBin ...
- loadrunner乱码问题解决办法
7.LoadRunner回放脚本时,在浏览器显示的中文是乱码 最近,遇到了好多乱码的问题,解决了一些,还有最后一个乱码,能想到的各种办法都试过了,还是不行,很奇怪啊. 解决这些乱码时,涉及到了http ...
- neo4j中索引的使用
neo4j可以对node和relationship中的属性建立索引,索引中的node(relationship)和属性对key-value为多对多的关系.一个node(relationship)可以在 ...
- 转:gcc编译C++程序
转:http://blog.csdn.net/liujiayu2/article/details/49864381 单个源文件生成可执行程序 下面是一个保存在文件 helloworld.cpp 中一个 ...
- db2部署与数据仓库应用
概念特性 安装 基础命令 连接 监控 存储过程 数据合并 Merge Into是增量备份 结果集分组 row_number() OVER (PARTITION BY COL1 ORDER BY COL ...
- cdoj1092-韩爷的梦 (字符串hash)【hash】
http://acm.uestc.edu.cn/#/problem/show/1092 韩爷的梦 Time Limit: 200/100MS (Java/Others) Memory Limi ...
- Cookie的用法
string strCookie=""; //创建一个名为user HttpCookie userCookie=new HttpCookie("user"); ...
- CentOS下Supervisor的安装与使用入门
[转载]http://www.51bbo.com/archives/2120 Supervisor是一个进程管理工具,官方的说法 用途就是有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原 ...
- 「Codechef April Lunchtime 2015」Palindromeness
「Codechef April Lunchtime 2015」Palindromeness 解题思路 : 考虑对于回文子串 \(s\) 贡献的定义: \[ value_s = [\ s[1,\lflo ...