PAT 1033. To Fill or Not to Fill (25)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1033
此题是一道贪心算法题,难度较大,关键在于贪心策略的选择:
#include <cstdio>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std; struct GasStation
{
double price;
double distance;
bool operator<(const GasStation& rhs) const
{
return distance<rhs.distance;
}
}; vector<GasStation> gasStations;
int _tmain(int argc, _TCHAR* argv[])
{
double Cmax,D,Davg;
int N;
scanf("%lf %lf %lf %d",&Cmax,&D,&Davg,&N);
GasStation station;
int i;
for(i=;i<N;++i)
{
scanf("%lf %lf",&station.price,&station.distance);
gasStations.push_back(station);
}
//将目的地当做最后一个加油站,距离为D,价格为0,以便贪心选择时总是朝着目的地前进
station.distance=D;
station.price=;
gasStations.push_back(station);
sort(gasStations.begin(),gasStations.end());
if(gasStations[].distance>)
{
printf("The maximum travel distance = 0.00\n");
return ;
}
double curGas=0.0,minSpend=0.0,minPrice;
const double driveLimit=Cmax*Davg;
int j,pivot;
for(i=;i<N;) //在前N个加油站都有可能加油
{
if(gasStations[i+].distance-gasStations[i].distance>driveLimit)
{
printf("The maximum travel distance = %.2lf\n",gasStations[i].distance+driveLimit);
return ;
}
pivot=i;
minPrice=gasStations[i].price;
//如果有油,找到不加油就能开到的比当前加油站便宜的加油站,直接开到那里加油
double curDis=curGas*Davg;
for(j = i+;j<=N && gasStations[j].distance-gasStations[i].distance <= curDis;++j)
{
if(gasStations[j].price<minPrice)
{
minPrice=gasStations[j].price;
pivot=j;
}
}
if(pivot!=i)
{
curGas-=(gasStations[pivot].distance-gasStations[i].distance)/Davg;
i=pivot;
continue;
} //以当前邮箱里面的油能跑到的范围内没有比当前加油站更便宜的加油站,于是在当前加油站后跑到第一个比当前
//加油站便宜的加油站
for(j=i+;j<=N && gasStations[j].distance-gasStations[i].distance <= driveLimit;++j)
{
if(gasStations[j].price<minPrice)
{
pivot=j;
break;
}
}
if(pivot!=i)
{
minSpend+=((gasStations[pivot].distance-gasStations[i].distance)/Davg-curGas)*gasStations[i].price;
i=pivot;
curGas=;
continue;
} //就算加满油后也不能跑到一个比当前加油站更便宜的加油站,则在加满油后跑到能跑到的加油站里面最便宜的一个
minPrice=INT_MAX;
for(j=i+;j<=N && gasStations[j].distance-gasStations[i].distance<=driveLimit;++j)
{
if(gasStations[j].price<minPrice)
{
minPrice=gasStations[j].price;
pivot=j;
}
}
minSpend+=(Cmax-curGas)*gasStations[i].price;
curGas=Cmax-(gasStations[pivot].distance-gasStations[i].distance)/Davg;
i=pivot;
}
printf("%.2lf\n",minSpend); return ;
}
PAT 1033. To Fill or Not to Fill (25)的更多相关文章
- 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 ...
随机推荐
- sqlserver分页;mysql分页;orcale分页 的sql 查询语句
一,sqlserver分页: )) ID FROM 表名 AS 表名1_1 ORDER BY ID)) ORDER BY ID; 二:mysql分页 )*每页显示的数据条数,当前页码 * 每页显示的数 ...
- responsive web design
http://d.alistapart.com/responsive-web-design/ex/ex-site-flexible.html http://alistapart.com/article ...
- javaweb学习总结(四十一)——Apache的DBUtils框架学习
一.commons-dbutils简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化 ...
- NuGet学习笔记(1)——初识NuGet及快速安装使用
关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...
- Drawable(2)State list Drawable Resource介绍
State List A StateListDrawable is a drawable object defined in XML that uses a several different ima ...
- 获取本机的ip
https://4sysops.com/archives/ipv6-tutorial-part-6-site-local-addresses-and-link-local-addresses/ In ...
- E212: 不能以写入模式打开 linux
跟大家讲解下事情发生的情况 背景:linux下有多个用户 每个用户有自己的密码 还有root用户 事由:我用zhengdengping用户登录 想去拷贝点 用户 lixianchu ...
- linux 失败无连接 检查电缆吗
将BOOTPROTO=dhcp改成 BOOTPROTO=static 改成手动获取IP的模式 原因: 虚拟机中的Linux目前是默认设成的自动获取IP设置,但你的网络中没有DHCP服务,所以会显示“正 ...
- Webform——购物车
购物车主要实现的功能: ①在主页面可以将所有商品显示出来,包括价格,库存. ②点击购买可以累加产品,如果是同一种产品,只会累加每种产品的数量. ③查看购物车,可以查看明细,包括所购物品的名称,价格,数 ...
- SQL Server查询性能优化——覆盖索引(二)
在SQL Server 查询性能优化——覆盖索引(一)中讲了覆盖索引的一些理论. 本文将具体讲一下使用不同索引对查询性能的影响. 下面通过实例,来查看不同的索引结构,如聚集索引.非聚集索引.组合索引等 ...