题目地址: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)的更多相关文章

  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. input标签文字点击变颜色

    <input type="text" class="ser_input"value="从这里搜索(^_^)" onfocus=&quo ...

  2. CSS display 属性详解

    定义display 属性设置是否及如何显示元素. 继承性: No 说明 这个属性用于定义建立布局时元素生成的显示框类型.对于 HTML 等文档类型,如果使用 display 不 谨慎会很危险,因为可能 ...

  3. var a=[]; 和 var a=new Array(); 的区别,为什么前者效率高

    因为 JSON格式的语法是引擎直接解释的.而new Array 则需要调用Array的构造器.还有就是1.当你需要将一个数字转化为字符串时可以这样定义:var s=""+1; 这样 ...

  4. POJ3714+最近点对

    特判标记即可 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h& ...

  5. php获取请求的方式(get/post)

    $request_method = $_SERVER['REQUEST_METHOD'];//用什么方式访问 get post

  6. IDEA 使用 SVN的一个注意点

    IDEA是调用SVN.EXE来实现相关版本管理功能的,所以必须要安装visualSVN,然后再使用相关功能!

  7. http://www.tuicool.com/articles/RzUzqei

    http://www.tuicool.com/articles/RzUzqei http://www.cnblogs.com/piaolingzxh/archive/2015/01/01/419783 ...

  8. unity博文搜集

    一.综合篇 1. 脚本 unity3d脚本编程基础 2.Mecanim 使用Mecanim实现连击 3. 数学图形学 U3D需要用到的数学基础  2 4. shader 猫都能学会的Unity3D S ...

  9. HashMap与HashTable联系与区别

    HashMap与HashTable 1.hashMap去掉了HashTable 的contains方法,但是加上了containsValue()和containsKey()方法. 2.hashTabl ...

  10. Mysql常用show命令,show variables like xxx 详解,mysql运行时参数

    MySQL中有很多的基本命令,show命令也是其中之一,在很多使用者中对show命令的使用还容易产生混淆,本文汇集了show命令的众多用法. 详细: http://dev.mysql.com/doc/ ...