PAT A 1033 To Fill or Not to Fill

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​​ (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. 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.

知识点:

贪心算法

思路:

针对所处的每一个加油站,有如下情况:

  • 在可以开到的范围内,有更便宜的加油站:

    • 本站加到刚好够开到下一个加油站;下一个加油站是最近的一个比本站便宜的
  • 在可以开到的范围内,没有更便宜的加油站:
    • 本站加满油,下一个加油站是最便宜且,最远的
  • 在可以开到的范围内,没有加油站:
    • 在本站加满油,能开多远开多远
  • 特殊情况:
    • d == 0 花费为0
    • 起点没有加油站 最远走0
 #include <iostream>
#include <algorithm>
using namespace std;
const int maxn = ; double cmax,d,davg;
int n;
struct GSType{
double dist;
double price;
};
struct GSType GS[maxn]; bool cmp(struct GSType a, struct GSType b){
return a.dist<b.dist;
} int main(int argc, char *argv[]) { scanf("%lf %lf %lf %d",&cmax,&d,&davg,&n);
for(int i=;i<n;i++){
scanf("%lf %lf",&GS[i].price,&GS[i].dist);
}
GS[n].dist = d;
GS[n].price = 0.0;
sort(GS, GS+n, cmp); if(d==){ // 特殊情况 起点即终点
printf("0.00\n");
return ;
}
if(GS[].dist!=){ // 特殊情况 起点没有加油站
printf("The maximum travel distance = 0.00\n");
return ;
} //printf("%d\n",GS[0].dist);
double tol_w = 0.0;
double Df = cmax*davg;
double LongestD = 0.0;
double remain = 0.0; int locate = ;
while(locate!=n){
double cheapest = 99999999.9;
int next_locate = -;
bool cheaper = false;
for(int i=locate+;i<=n&&(GS[locate].dist)<GS[i].dist&&GS[i].dist<=(GS[locate].dist+Df);i++){
if(GS[i].price<GS[locate].price){ //有便宜的,找最近的
next_locate = i;
cheaper = true;
break;
}
if(GS[i].price<=cheapest){ // 没有比便宜的,找最便宜的
cheapest = GS[i].price;
next_locate = i;
}
}
if(next_locate == -){ // 没有能到的
LongestD = GS[locate].dist+cmax*davg;
//printf("%f\n",GS[locate].dist);
printf("The maximum travel distance = %.2f\n",LongestD);
return ;
}
if(cheaper){
if((GS[next_locate].dist-GS[locate].dist)/davg>remain){
tol_w += ((GS[next_locate].dist-GS[locate].dist)/davg-remain)*GS[locate].price;
remain = ;
}else{
remain -= (GS[next_locate].dist-GS[locate].dist)/davg;
}
}else{
tol_w += (cmax-remain)*GS[locate].price;
remain = cmax-(GS[next_locate].dist-GS[locate].dist)/davg;
}
locate = next_locate;
}
printf("%.2f\n",tol_w);
}

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

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

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

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

  4. PAT甲级1033. To Fill or Not to Fill

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

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

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

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

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

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

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

随机推荐

  1. springboot 日志1

    技术交流群: 816227112 Spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J,  ...

  2. sqlite 数据库错误 The database disk image is malformed database disk image

    收银机上的sqlite数据库经常出现这种错误,错误的原因有可能是突然断电或是一些不规范操作导致的. 网上一般的做法有两种: 方法一: 1.在https://www.sqlite.org/downloa ...

  3. Struts2框架的数据封装一之属性封装(属性封装的第二种方式:封装成javaBean)

    Struts2中提供了两类数据封装的方式? 第一种方式:属性驱动(有两种方式:一个对属性,另外一个是将参数封装到javaBean中) B. 在页面上,使用OGNL表达式进行数据封装.(将参数封装到ja ...

  4. django中的时区设置TIME_ZONE,USE_TZ

    Django如果开启了Time Zone功能,则所有的存储和内部处理,甚至包括直接print显示全都是UTC的.只有通过模板进行表单输入/渲染输出的时候,才会执行UTC本地时间的转换. 所以我建议后台 ...

  5. python string tuple list dict 相互转换的方法

    dict = {'name': 'Zara', 'age': 7, 'class': 'First'}# 字典转为字符串,返回:<type 'str'> {'age': 7, 'name' ...

  6. 1.1 Java 的概述

    [什么是java]:Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是有SunMicrosystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaSE,Jav ...

  7. c++文件中引用C代码

    下面提供一个比较完整的示例程序,一共有四个文件:main.cpp.test.c.test.h.test.hpp main.cpp #include "test.hpp" int m ...

  8. MySQL自带的4个数据库

    安装完 MySQL 后会发现有四个自带的数据库: information_schema -- 该数据库保存了 MySQL 服务器所有数据库的信息.比如数据库的名称.数据库中的表名称.访问权限.数据库中 ...

  9. Getting svn to ignore files and directories

    August 27, 2013Software Developmentresources, subversion, svn, tutorial, version control Who knew it ...

  10. linux上的工具或软件

    1.下载软件 yum install axelaxel http://mirror.cse.iitk.ac.in/archlinux/iso/2015.04.01/archlinux-2015.04. ...