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. elasticsearch6.7 05. Document APIs(3)GET API

    2.GET API get API 可以通过文档id从索引中获取json格式的文档,以下示例从twitter索引中获取type为_doc,id值为0为的JSON文档: GET twitter/_doc ...

  2. 【Java并发编程】13、forkjoin

    http://www.infoq.com/cn/articles/fork-join-introduction http://www.importnew.com/14506.html Java7中的F ...

  3. html页面背景设定相关

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. JavaScript有这几种测试

    译者按: 也许你讨厌测试,但是你不得不面对它,所以至少区分一下单元测试.集成测试与功能测试?对吧... 原文: What are Unit Testing, Integration Testing a ...

  5. js判断数据类型的四种方法

    1.typeof typeof是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型.返回的结果用该类型的字符串(全小写字母)形式表示,包括number,string,boolean,und ...

  6. 不固定个数组,进行一一对应的组合,js将多个数组实现排列组合

    var arr = [ ["a", "b"], ["1", "2"], ["d"] ]; var s ...

  7. Scoop及使用

    scoop window的命令行安装程序管理工具 通常我们下载软甲需要从网上搜索软件并下载安装等一系列复杂麻烦的过程 现在有了scoop 等一系列包管理器的诞生,就是省去了上述繁琐的搜索 - 下载 - ...

  8. 怎么在ReactNative里面使用Typescript

    今天来搞一搞怎么搭建一个可以使用Typescript的ReactNative环境好吧,一句废话不多说,直接开始好吧 1.全局安装create-react-native-app yarn global ...

  9. Maven 安装 on centos7

    本文演示如何在CentOS7上安装maven. 1 准备工作 1.1 进入官网下载栏目 http://maven.apache.org/download.cgi 找到下载列表中 Binary tar. ...

  10. 06-OpenLDAP密码策略

    阅读视图 openldap密码策略 OpenLDAP服务端定制密码策略 客户端策划策略实例 定义用户第一次登录就修改密码 问题排查手册 重点推荐官方文档 备注:本文依然承接系列文. 1. openld ...