题目描写叙述

With highways available, driving a car from Hangzhou to any other city is easy.  But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time.  Different gas station may give different price.  You are asked to carefully design the cheapest route to go.

输入描写叙述:

Each input file contains one test case.  For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations.  Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N.  All the numbers in a line are separated by a space.

输出描写叙述:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places.  It is assumed that the tank is empty at the beginning.  If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

输入样例:

50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300

输出样例:

749.17
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <climits> using namespace std; typedef struct Node
{
double price;
double distance;
}Node; bool cmp(const Node& lhs, const Node& rhs)
{
return lhs.distance<rhs.distance;
} int main()
{
double Cm,D,cur,cost,minCost,maxDistance;
int Da,N;
int i,j,k,l,ans;
while(cin>>Cm>>D>>Da>>N)
{
maxDistance=Cm*Da;
Node *node=new Node[N+1];
for(i=0;i<N;i++)
cin>>node[i].price>>node[i].distance; sort(node,node+N,cmp); node[N].price=0;
node[N].distance=D; ans=cur=cost=0;
for(i=0;i<N-1;i++)
{
if((node[i+1].distance-node[i].distance)>maxDistance)
break;
}
if(i<N-1)
{
printf("The maximum travel distance = %.2lf\n",node[i].distance+maxDistance);
}
else
{
for(i=0;i<N;)
{ //后面假设有比当前更廉价的汽油,如今先少弄点,够到那个地方就OK
l=i;
minCost=node[i].price;
for(j=i+1;j<=N && (node[i].distance+maxDistance>=node[j].distance);j++)
{
if(minCost>node[j].price)
{
l=j;
break;
}
}
if(l!=i)
{
cost=cost+((node[l].distance-node[i].distance)/Da-cur)*node[i].price;
cur=0;
i=l;
continue;
}
//假设找不到更廉价的加油站。找出相对最廉价的加油站。而且在当前加油站加满油
l=i;
minCost=1e18;
for(j=i+1;j<=N && (node[i].distance+maxDistance>=node[j].distance);j++)
{
if(minCost>node[j].price)
{
minCost=node[j].price;
l=j;
}
}
cost=cost+(Cm-cur)*node[i].price;
cur=Cm-(node[l].distance-node[i].distance)/Da;
i=l;
}
printf("%.2lf",cost);
}
/* for(i=0;i<N;i++)
cout<<node[i].price<<" "<<node[i].distance<<endl; */
}
return 0;
}

贴个图

PAT 1004 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. 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 ...

  8. 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 ...

  9. 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 ...

  10. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

随机推荐

  1. 深入分析Android动画(二)

    上回书说到Android动画的分类以及基本使用,这会书主要说Android属性动画的原理,对于View动画的原理本篇不做深入分析.对于Android动画的基础请看深入分析Android动画(一) 我们 ...

  2. Android使用百度地图定位并显示手机位置后使用前置摄像头“偷拍”

    今天老板让我验证一下技术可行性,记录下来. 需求 :定位手机的位置并在百度地图上显示,得到位置后使用前置摄像头进行抓拍 拿到这个需求后,对于摄像头的使用不太熟悉,于是我先做了定位手机并在百度地图上显示 ...

  3. ML技术 - 特征选择

    1. 决策树中的特征选择 分类决策树是一种描述对实例进行分类的树型结构,决策树学习本质上就是从训练数据集中归纳出一组分类规则,而二叉决策树类似于if-else规则.决策树的构建也是非常的简单,首先依据 ...

  4. Delphi工程版本号修改工具

    自动修改某目录下符合条件的Delphi工程(dproj)版本号, 支持命令行调用支持通配符忽略文件 -p [Path] 在[Path]路径下查询所有dproj文件(可以为空, 默认路径为程序当前路径) ...

  5. Python - SIP参考指南 - 介绍

    介绍 本文是SIP4.18的参考指南.SIP是一种Python工具,用于自动生成Python与C.C++库的绑定.SIP最初是在1998年用PyQt开发的,用于Python与Qt GUI toolki ...

  6. C#中的Unsafe和Fixed

    托管代码 (managed code):由公共语言运行库环境(而不是直接由操作系统)执行的代码.托管代码应用程序可以获得公共语言运行库服务,例如自动垃圾回收.运行库类型检查和安全支持等.这些服务帮助提 ...

  7. bootstrap-wysihtml5设置值

    今天做项目的时候用到了 bootstrap-wysihtml5,添加完一篇文章返回去继续添加的时候,js已经设置了将所有变量的值清空,但是不管用$('#someId').val(")还是$( ...

  8. [转]Oracle 重建索引的必要性

    http://blog.csdn.net/leshami/article/details/23763963 索引重建是一个争论不休被不断热烈讨论的议题.当然Oracle官方也有自己的观点,我们很多DB ...

  9. 使用AspectCore动态代理

    前言 最近越来越多的同学关注到AspectCore,并且提出不少中肯的建议,其中最多的提议是希望能够看到更多的关于AspectCore使用方式的文章和Demo.那么在这篇文章里,我们就来聊聊Aspec ...

  10. Python StringIO与BytesIO、类文件对象

    StringIO与BytesIO StringIO与BytesIO.类文件对象的用途,应用场景,优.缺点. StringIO StringIO 是io 模块中的类,在内存中开辟的一个文本模式的buff ...