PAT_A1033#To Fill or Not to Fill

时间:2022-09-04 18:10:11

Source:

PAT A1033 To Fill or Not to Fill (25 分)

Description:

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.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: C​max​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤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: P​i​​, the unit gas price, and D​i​​ (≤), the distance between this station and Hangzhou, for ,. All the numbers in a line are separated by a space.

Output Specification:

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.

Sample Input 1:

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

Sample Output 1:

749.17

Sample Input 2:

50 1300 12 2
7.10 0
7.00 600

Sample Output 2:

The maximum travel distance = 1200.00

Keys:

Code:

 /*
Data: 2019-07-23 19:26:36
Problem: PAT_A1033#To Fill or Not to Fill
AC: 56:21 题目大意:
旅途中各个加油站的价格不同,用最少的钱到达目的地;
或能够到达的最远距离
输入:
第一行给出,油箱最大容量Cmax<=100,目的地距离D<=3e4,单位油量行驶距离Davg<=20,加油站数量N<=500
接下来N行,价格p,距起点距离d 基本思路:
秉承各地加油站能少花钱则少花钱原则
预设:目的地油价=0,起始油量=0
前方油价低于当前油价,直接前往
(此时油量一定是不够的,否则上一轮前进时,就会到达该站,因此加适量油即可)
否则前往前方油价最低的加油站
(能够到达则直接去;不能到达则加满油,如果加恰好的油,那么到前方一定会加油,花的钱一定比现在多,所以现在加满)
*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int M=;
struct node
{
double p,d;
}gas[M]; bool cmp(const node &a, const node &b)
{
return a.d < b.d;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif double Cmax,D,Davg,bill=,tank=;
int n,now=;
scanf("%lf%lf%lf%d", &Cmax,&D,&Davg,&n);
for(int i=; i<n; i++)
scanf("%lf %lf", &gas[i].p, &gas[i].d);
sort(gas,gas+n,cmp);
gas[n] = node{,D};
if(gas[].d != )
{
printf("The maximum travel distance = 0.00");
n=;
}
for(int i=; i<n; i++)
{
if(gas[i].d+Cmax*Davg < gas[i+].d)
{
printf("The maximum travel distance = %.2f", gas[i].d+Cmax*Davg);
n=;break;
}
}
while(now<n)
{
int pos=now+,next=now+;
double mprice=gas[now].p;
while(gas[now].d+Cmax*Davg>=gas[pos].d)
{
if(gas[pos].p < mprice)
{
mprice = gas[pos].p;
next = pos;
break;
}
else if(gas[pos].p < gas[next].p)
next = pos;
pos++;
}
tank -= (gas[next].d-gas[now].d)/Davg;
if(mprice < gas[now].p)
{
bill += (-)*tank*gas[now].p;
tank=;
}
else if(tank < )
{
bill += (Cmax-tank-(gas[next].d-gas[now].d)/Davg)*gas[now].p;
tank = Cmax - (gas[next].d-gas[now].d)/Davg;
}
now = next;
}
if(n)
printf("%.2f", bill); return ;
}

PAT_A1033#To Fill or Not to Fill的更多相关文章

  1. 1033&period; To Fill or Not to Fill &lpar;25&rpar;

     题目链接:http://www.patest.cn/contests/pat-a-practise/1033 题目: 1033. To Fill or Not to Fill (25) 时间限制 1 ...

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

  3. 【贪心】PAT 1033&period; To Fill or Not to Fill &lpar;25&rpar;

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

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

  5. PAT甲级1033&period; To Fill or Not to Fill

    PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...

  6. PAT 1033 To Fill or Not to Fill&lbrack;dp&rsqb;

    1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other ...

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

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

  8. pat1033&period; To Fill or Not to Fill &lpar;25&rpar;

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

  9. PAT 甲级 1033 To Fill or Not to Fill &lpar;25 分&rpar;(贪心&comma;误以为动态规划&comma;忽视了油量问题)&ast;

    1033 To Fill or Not to Fill (25 分)   With highways available, driving a car from Hangzhou to any oth ...

随机推荐

  1. ASP&period;NET MVC学习之母版页和自定义控件的使用

    一.母板页_Layout.cshtml类似于传统WebForm中的.master文件,起到页面整体框架重用的目地1.母板页代码预览 <!DOCTYPE html> <html> ...

  2. 漫谈可视化Prefuse(六)---改动源码定制边粗细

    可视化一路走来,体会很多:博客一路写来,收获颇丰:代码一路码来,思路越来越清晰.终究还是明白了一句古话:纸上得来终觉浅,绝知此事要躬行. 跌跌撞撞整合了个可视化小tool,零零碎碎结交了众多的志同道合 ...

  3. spring-自动加载配置文件&bsol;使用属性文件注入

    在上一篇jsf环境搭建的基础上 , 加入spring框架 , 先看下目录结构 src/main/resources 这个source folder 放置web项目所需的主要配置,打包时,会自动打包到W ...

  4. Qt5 新特性

    Qt 5 已经临近发布,其最大的特点就是模块化.将原来庞大的模块更细分为不同的部分,同时,一个大版本的升级,当然少不了添加.删除各个功能类.文本简单介绍 Qt5 的特性,其具体内容来自 Qt5 官方 ...

  5. json时间格式化问题

    function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式 try { var date = new Date(parseInt(jsonDate.rep ...

  6. 总结一下工作中遇到的NPOI以及在ASP&period;NET MVC中的使用

    1.前言 相信大家在工作中经常要遇到一些导入导出Execl操作.学习贵在分享,分享使人快乐,园子里的前辈已经有很多好的文章,鄙人也是能力有限,在这里把这些好的文章总结,方便以后再工作中使用. NPOI ...

  7. Spring容器中的Bean

    一,配置合作者的Bean Bean设置的属性值是容器中的另一个Bean实力,使用<ref.../>元素,可制定一个bean属性,该属性用于指定容器中其他Bean实例的id属性 <be ...

  8. &sol;etc&sol;fstab一些信息

    [root@shine shine]# vim /etc/fstab ## /etc/fstab# Created by anaconda on Thu May 9 13:29:35 2013## A ...

  9. aliyun oss操作汇总

    // endpoint以杭州为例,其它region请按实际情况填写 String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; ...

  10. 关于使用deepin在linux下安装mysql出现Can&&num;39&semi;t connect to local MySQL server through socket &&num;39&semi;&sol;tmp&sol;mysql&sol;mysql&period;sock&&num;39&semi; &lpar;2&rpar;的解决方法

    根据目录/etc/mysql打开文件debain.cnf 此时文本里的内容为 # Automatically generated for Debian scripts. DO NOT TOUCH![c ...