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​​ (≤), the distance between this station and Hangzhou, for ,. 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
题目分析:一道贪心题 用最少的钱走最多的路径 不会 抄了柳神
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
struct station {
double price,dist;
};
bool compare(const station& a, const station& b)
{
return a.dist < b.dist;
}
int main()
{
double cmax = , d = , davg = ;
int n = ;
cin >> cmax >> d >> davg >> n;
vector<station> s(n + );
s[] = { 0.0,d };
double price,dist;
for (int i = ; i <= n; i++)
{
cin >> price >> dist;
s[i] = { price,dist };
}
sort(s.begin(), s.end(), compare);
double nowdist=, maxdist=,leftdist=,nowprice=, totalprice=;
if (s[].dist != )
{
printf("The maximum travel distance = 0.00");
return ;
}
else
nowprice = s[].price;
while (nowdist<d)
{
double minprice = INT_MAX, mindist = -;
maxdist = nowdist + cmax * davg;
int flag = ;
for (int i = ; i <= n&&s[i].dist<=maxdist; i++)
{
if (s[i].dist <=nowdist)continue;
if (s[i].price < nowprice)
{
totalprice += (s[i].dist - nowdist - leftdist) * nowprice / davg;
leftdist = ;
nowdist = s[i].dist;
nowprice = s[i].price;
flag = ;
break;
}
if (s[i].price < minprice)
{
minprice = s[i].price;
mindist = s[i].dist;
}
}
if (flag == && minprice != INT_MAX)
{
totalprice += (nowprice * (cmax - leftdist / davg));
leftdist = cmax * davg - (mindist - nowdist);
nowdist = mindist;
nowprice = minprice;
}
if (flag == && minprice == INT_MAX)
{
nowdist += cmax * davg;
printf("The maximum travel distance = %.2f", nowdist);
return ;
}
}
printf("%.2f", totalprice);
return ;
}

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

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

  4. 【贪心】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 ...

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

  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[dp]

    1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other ...

  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. 1033 To Fill or Not to Fill

    PAT A 1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other ...

随机推荐

  1. Spring、SpringMVC、SpringBoot、SpringCloud的区别和联系

    简介 Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架.Spring能够让我们编写出更简洁.易于管理.更易于测试的代码. SpringMVC是Spring的一个模块,一个we ...

  2. 分布式爬虫管理平台Crawlab安装与使用

    Why,为什么需要爬虫管理平台? 以下摘自官方文档: Crawlab主要解决的是大量爬虫管理困难的问题,例如需要监控上百个网站的参杂scrapy和selenium的项目不容易做到同时管理,而且命令行管 ...

  3. JVM系列八(虚拟机性能监控命令).

    jps JVM Process Status Tool,显示指定系统内所有的 HotSpot 虚拟机进程.显示信息包括虚拟机执行主类名称以及这些进程的本地虚拟机唯一ID(Local Virtual M ...

  4. C++ 判断两个圆是否有交集

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include <math.h> #include <easyx.h ...

  5. 小白的docker极简入门(二)、5分钟教你玩转docker安装

    0-前言 上一篇中,我们已经安装后Linux了,我们需要在Linux下安装docker,然后才能在docker中安装和部署各种应用 同样,5分钟教你完成docker正确安装和使用, 不是纸上谈兵,不是 ...

  6. go例子(二) 使用go语言实现数独游戏

    例子托管于github example.go package main import (     "./sudoku" ) func main() {     //var smap ...

  7. python获取群成员信息

    #coding: utf-8 import itchat,datetime from itchat.content import TEXT itchat.auto_login(hotReload=Tr ...

  8. dom4j解析xml格式文件实例

    以下给4种常见的xml文件的解析方式的分析对比: DOM  DOM4J  JDOM  SAX Dom解析    在内存中创建一个DOM树,该结构通常需要加载整个文档然后才能做工作.由于它是基于信息层次 ...

  9. TCP IP Socket In C, 2e-chapter 1 Introduction

    本章是基础概念,建议补计算机网络基础,这里不全. 目录 1 网络,数据包,协议 2 关于地址(address) 2.1 IP地址格式 2.2 IPv4和IPv6共存 2.3 端口号 2.4 特殊地址 ...

  10. 启动/关闭oracle服务

    写一个脚本就可以很方便的打开服务和关闭服务,但是记住要以管理员的身份运行哦! 首先建立两个文件:启动.txt 和关闭.txt [在下面代码里启动的服务名称可能版本不一样,有可能名字不一样,自己对照看看 ...