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, ...
随机推荐
- Pseudocode MD5 CODE
//Note: All variables are unsigned 32 bit and wrap modulo 2^32 when calculating var int[64] s, K //s ...
- sql server: left join 重复数据
---涂聚文 2017-9-28 SELECT VipExamMailProjectId,VipExamMailStaffID FROM VipExamMailRecord WHERE VipExam ...
- element-ui 组件源码分析整理笔记目录
element-ui button组件 radio组件源码分析整理笔记(一) element-ui switch组件源码分析整理笔记(二) element-ui inputNumber.Card .B ...
- 【代码笔记】Web-HTML-列表
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- 一个Web页面的问题分析
几个月之前我接到一个新的开发任务,要在一个旧的Web页面上面增添一些新的功能.在开发的过程中发现旧的代码中有很多常见的不合适的写法,结合这些问题,如何写出更好的,更规范的,更可维护的代码,就是这篇文章 ...
- JMeter 集合点设置之Synchronizing Timer的使用
集合点设置之Synchronizing Timer的使用 by:授客 QQ:1033553122 1.布局设置 注: 1) 说明: 名称:自定义名称 Number of Simulated Users ...
- Android jni c/c++线程通过CallVoidMethod调用java函数出现奔溃问题
最近在移植网络摄像机里的p2p库到android平台,需要用到jni,最近在c线程了调用java函数的时候 出现一个问题,假如在同一个线程调用java函数是没问题的,但在一个c线程了调用java函数就 ...
- Android RecycleView 自定义Item的使用
自定义布局的RecycleView需要自己实现Adapter,ViewHolder和布局: 自定义Adapter继承RecycleView.Adapter,重写getItemCount(),onBin ...
- Python:GUI之tkinter学习笔记1控件的介绍及使用
相关内容: tkinter的使用 1.模块的导入 2.使用 3.控件介绍 Tk Button Label Frame Toplevel Menu Menubutton Canvas Entry Mes ...
- 超简单,Centos7 安装 rabbitMQ
首先声明,本人是Linux新手一枚.经历了在阿里云Centos上部署rabbitMQ与重装的痛苦经历,后多方查找终于找到了简单方法.Linux高人来说请跳过本篇文章,新手可以试试. 1.设置Cento ...