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

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

#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std;
float f[] = { - };
int main() {
fill(f, f + , -1.0);
int cmax, d, da, n, flag = , out = -;
scanf("%d %d %d %d", &cmax, &d, &da, &n);
for (int i = ; i < n; i++) {
int dis;
float p;
scanf("%f %d", &p, &dis);
if(dis<d)f[dis] = p;
}
int now = ;
float total = ;
float need = , remain = ;
int can = da * cmax;
if (f[] == -) {
printf("The maximum travel distance = %d.00", now);
return ;
}
while (now < d) {
int i, flag1 = , flag2 = ;
float min = 99999999.0;
for (i = ; i <= can; i++) {
if(now+i<=d){
if (f[i + now] != -) {
if (f[i + now] <= f[now]) {
flag2 = i;
break;
}
else {
if (f[i + now] < min) {
flag1 = i;
min = f[i + now];
}
}
}
}
else {
flag1 = ;
break;
}
}
if (flag1 == && flag2 == && now + can < d) {
now += can;
printf("The maximum travel distance = %d.00", now);
system("pause");
return ;
}
else if (flag2 != ) {
need = flag2 - remain;
remain = ;
total += need * f[now]/da;
now += flag2;
}
else if (flag1 != ) {
need = cmax * da - remain;
total += need * f[now]/da;
remain = cmax * da - flag1;
now += flag1;
}
else {
total += f[now] * (d - now)/da;
now = d;
}
}
printf("%.2f", total);
system("pause");
return ;
}

注意点:一道贪心算法题,有点麻烦,分几种情况,

1、能到最大距离里没有加油站的

2、能到的加油站比现在的便宜,就只加到那里的油

3、能到的加油站都比现在的贵,就要加满油箱

4、能到距离没加油站但到终点了

其中第三点贪心要注意,一开始没考虑,怎么算都不对。

PAT A1033 To Fill or Not to Fill (25 分)——贪心的更多相关文章

  1. PAT 甲级 1067 Sort with Swap(0, i) (25 分)(贪心,思维题)*

    1067 Sort with Swap(0, i) (25 分)   Given any permutation of the numbers {0, 1, 2,..., N−1}, it is ea ...

  2. PAT甲级:1066 Root of AVL Tree (25分)

    PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...

  3. PAT 1033 To Fill or Not to Fill (25分) 贪心思想

    题目 With highways available, driving a car from Hangzhou to any other city is easy. But since the tan ...

  4. 1033 To Fill or Not to Fill (25分)(贪心)

    With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...

  5. PAT 甲级 1002 A+B for Polynomials (25 分)

    1002 A+B for Polynomials (25 分) This time, you are supposed to find A+B where A and B are two polyno ...

  6. PAT A1106 Lowest Price in Supply Chain (25 分)——树的bfs遍历

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  7. PAT A1097 Deduplication on a Linked List (25 分)——链表

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...

  8. pat 1006 Sign In and Sign Out(25 分)

    1006 Sign In and Sign Out(25 分) At the beginning of every day, the first person who signs in the com ...

  9. PAT 甲级 1079 Total Sales of Supply Chain (25 分)(简单,不建树,bfs即可)

    1079 Total Sales of Supply Chain (25 分)   A supply chain is a network of retailers(零售商), distributor ...

  10. PAT Advanced 1006 Sign In and Sign Out (25 分)

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

随机推荐

  1. IDEA从SVN中导入多模块项目

    idea多模块项目的导入 在IntelliJ IDEA中间新建一个空项目,名字可以是svn要检索的的项目名称,原则是开心就好: 现在,我们将我们在svn中的多模块的项目down下来 选中顶部的导航中的 ...

  2. Java基础——正则表达式

    一.什么是正则表达式 正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表通常被用来检索.替换那些符 ...

  3. MyBatis学习笔记(三) Configuration类

    一.初探Configuration类 我们先来看一下MyBatis的XML配置文件的结构,(摘自mybatis.org) 下面这个是Configuration类的部分变量 一点不一样是不是??? 其实 ...

  4. elasticsearch6.7 01.入门指南(3)

    4.Modifying Your Data(修改数据) Elasticsearch 提供了近实时的操纵数据和搜索的能力.默认情况下,从索引/更新/删除数据到在搜索结果中显示数据会有 1 秒的延迟(刷新 ...

  5. 【高并发解决方案】7、HAProxy安装和配置

    简介 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要 ...

  6. 写出java8实现对List<User>中的username字段过滤出不等于张三的数据

    写出java8实现对List<User>中的username字段过滤出不等于张三的数据... 对...这个是一道面试题.当时没有看过java8的新特性...所以有点懵. 看完之后感觉 真. ...

  7. 网页三剑客:HTML+CSS+JavaScript 之 HTML的概述

    HTML(超文本标记语言) 什么是HTML? HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言: HyperText Markup Language HTML 不是一种编程语言,而 ...

  8. 纯CSS绘制mac代码

    1.效果图 2.代码 <!doctype html> <html lang="en"> <head> <meta charset=&quo ...

  9. loadrunner 场景设计-添加Unix、Linux Resources计数器

    场景设计-添加Unix.Linux Resources计数器 by:授客 QQ:1033553122 A.   目的 监控要测试的Unix.Linux服务器的资源使用情况 Linux CentOS为例 ...

  10. Retrofit 代理模式

    https://blog.csdn.net/H176Nhx7/article/details/78139406