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 ...
随机推荐
- IIS Handler and Module探索
Create Handler & Module Run the Visual Studio Create a Class Library “HMHandler” --> Change t ...
- oracle----修改表中的数据
1. 修改表中的数据:UPDATE语句: 语法: UPDTAE table_name SET column1 = value1,... [WHERE conditions] (2),无条件的更新(没有 ...
- PHP glob() 函数
定义和用法 glob() 函数返回匹配指定模式的文件名或目录. 该函数返回一个包含有匹配文件 / 目录的数组.如果出错返回 false. 语法 glob(pattern,flags) 参数 描述 fi ...
- UVA 11734 Big Number of Teams will Solve This
大水题,不解释啦! #include<cstdio> #include<cstring> #define maxn 50 using namespace std; char s ...
- thinkphp 调用函数
1,定义为Common.php文件.自动加载. 2,配置文件config.php文件里配置'LOAD_EXT_FILE'=>'function'.则会自动加载function.php文件 3,使 ...
- 命令行静态编译QT程序
在.pro文件里加上(非必须)CONFIG += static release windows 静态库必须放在这个路径:C:\Qt\Qt5.3.2_static\bin\qmake -makefile ...
- 【HDOJ】2086 A1 = ?
数学题,首先推导出2*sum{c1,c2...cn} = (An+1-An) - (A1-A0),在将n个该式相加,可以推导出(n+1)*A1=An+1+n*A0-2*sum{sum{c1,c2... ...
- tlplayer for ios V1.1(附上截图)
此程序UI修改于虎跃在线课堂.所以极其相似. 可以播放网络视频与本地视频,不知道怎么拷贝本地视频到Ipad或iphone上看的朋友,请自己到网上看教程. 支持mms,file,rtsp,rtmp,ht ...
- bzoj1266
第一问不谈, 第二问首先我们要找出哪些是s到t的最短路上的边 由于是无向图,首先正反两遍最短路,求出是s到任意点的距离,任意点到t的距离(即t到任意点的距离): 然后穷举每条边判断是否在最短路上用d[ ...
- BZOJ2843: 极地旅行社
2843: 极地旅行社 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 90 Solved: 56[Submit][Status] Descripti ...