PAT A1033 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 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: 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 Nlines 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.
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 分)——贪心的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- PAT A1106 Lowest Price in Supply Chain (25 分)——树的bfs遍历
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- 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 ...
- 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 ...
- 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 ...
- 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, ...
随机推荐
- Apache SkyWalking的架构设计【译文】
Apache SkyWalking提供了一个功能强大并且很轻量级的后端.在此,将介绍为什么采用以下方式来设计它,以及它又是如何工作的. 架构图 对于APM而言,agent或SDKs仅是如何使用libs ...
- python全局解释器锁(GIL)
文章作者:卢钧轶(cenalulu) 本文原文地址:http://cenalulu.github.io/python/gil-in-python/ ,对文章做了适当的修改,加入了一些自己的理解. CP ...
- csharp: FTP Client Library using System.Net.FtpClient and FluentFTP,测试中存在的BUG修正
https://netftp.codeplex.com/ /// <summary> /// Gets a file listing from the server. Each FtpLi ...
- Java网络编程--InetAdress类
一.地址 java.net包中的InetAddress 类对象含有一个Internet主机地址的域名和Ip地址 www.sina.com.cn/202.108.35.210 二.获取地址 1.获取In ...
- engineecms——工程师知识管理系统,带文档协作和状态和流程
工程师们工作的平台,需要对图纸资料按工程项目名称,按工程项目阶段,按参与设计的专业等来分放. 而设计成果需要进行校审,才能算最终成果,也就是要具备状态和流程功能,这个一般网盘资源存储是做不到的. 一些 ...
- linux网络 skb_buff
sbk_buff中的data_len指的是尾部带的page数据的长度,len指的是总共的data的长度,len-data_len是第一个线性buf的数据长度. sk_buff->len:表示当前 ...
- (网页)20个JS 小技巧超级实用
转自CSDN: 1. 将彻底屏蔽鼠标右键 oncontextmenu=”window.event.returnValue=false”< table border oncontextmenu=r ...
- python常用模块之pickle
python1个json模块和1个pickle模块都是用于序列化的模块. 其中前文介绍过json模块.json模块只能序列化普通字符,比如字典.列表等.但是如果是函数呢?json其实是无法序列化的.这 ...
- Linux MySql 安装与配置
为什么选择MySQL数据库? 毫无疑问,绝大多数的使用linux操作系统的大中小型互联网网站都在使用MySQL作为其后端的数据库存储,从大型的BAT门户,到电商平台,分类门户等无一例都使用MySQL数 ...
- OffsetDateTime工具类
java8时间API介绍及使用 java7及之前版本,推荐使用jodatimejava8提供的datetime api参考了jodatime 在新的时间API中, Instant表示一个精确的时间点, ...