题目描写叙述

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. vue.js项目安装

    Vue.js 安装 NPM 方法安装vue.js项目 npm 版本需要大于 3.0,如果低于此版本需要升级它: # 查看版本 $ npm -v 2.3.0 #升级 npm npm install np ...

  2. 解决webstorm启动索引文件卡死问题

    问题 当目录下的文件数量较大时,用webstorm打开会出现卡顿,甚至卡死现象,例如:node_modules目录 解决方案 不让webstorm索引该目录下的文件步骤:1.node_modules目 ...

  3. [转载] 2 分钟读懂大数据框架 Hadoop 和 Spark 的异同

    转载自https://www.oschina.net/news/73939/hadoop-spark-%20difference 谈到大数据,相信大家对Hadoop和Apache Spark这两个名字 ...

  4. web前端-----第一弹html

    HTML 初识 web服务本质 import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s ...

  5. 自定义统一api返回json格式(app后台框架搭建三)

    在统一json自定义格式的方式有多种:1,直接重写@reposeBody的实现,2,自定义一个注解,自己去解析对象成为json字符串进行返回 第一种方式,我就不推荐,想弄得的话,可以自己去研究一下源码 ...

  6. http2.4简单配置

    前言: 上一篇博文说到了http的发展以及http完整请求响应的工作流程. 一.开篇: 从最简单的静态服务器开始. 之前说过,http是应用层协议,必定会在用户空间体现出具体的应用程序.常见的http ...

  7. Mysql相关知识点总结(一)

    information_schema库:information_schema库中的表大都是temporory表,都是只读的,不能进行更新.删除和插入等操作,也不能加触发器,因为它们实际只是一个视图,不 ...

  8. MeshRenderer组件及相关API

    MeshRenderer:网格过滤器,用于"渲染"显示模型. Cast Shodows:是否投射阴影.(on:开.off:关) Receive Shodows:是否接收阴影. Ma ...

  9. python学习笔记 list

    1.list中的任一元素可以是任一类型.可以是混合的,如,前两个字符串后面的是数字.都是可以的. 2.可以用-1表示最后一个元素. 3.注意不要越界. 4.len(mates) 用来计算list的大小 ...

  10. View处理常用方法封装

    处理View常用的一些方法:Drawable和Bitmap互相转换,Bitmap改变大小,dp.px互相转换,sp.px互相转换,根据Id查找Drawable,获取屏幕大小等方法. import an ...