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. 回想笔记 瞎比比 域名注册 解析绑定ip 下载证书 设置证书 重定向http到https请求

    2019.7.27 回想笔记 拥有腾讯云服务器一台 阿里云注册5元域名,进行备案 完成之后 使用解析 绑定服务器ip地址 ,使用域名可以访问到web服务器而不是通过直接暴露ip地址进行访问 证书购买 ...

  2. 安卓手机tcpdump的使用

    一.常规操作步骤 1. 手机要有root权限 2. 下载tcpdump http://www.strazzere.com/android/tcpdump 3. adb push c:\wherever ...

  3. (转)const的内部链接属性(C++中适用)

    转载自:http://xiangwangfeng.com/2011/05/02/const%E7%9A%84%E5%86%85%E9%83%A8%E9%93%BE%E6%8E%A5%E5%B1%9E% ...

  4. libfastcommon总结(一)加载主机上所有网卡的IPv4的地址

    头文件为local_ip_func.h 主要接口 load_local_host_ip_addrs();//加载主机网口所有IPv4地址到列表    print_local_host_ip_addrs ...

  5. MySQL 【教程二】

    MySQL 创建数据表 创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以下为创建MySQL数据表的SQL通用语法: # CREATE TABLE table_name (c ...

  6. 洛古 P1312 Mayan游戏(dfs+剪枝)

    题目链接 这道题和俄罗斯方块很像 很明显,我们可以看出这是一个dfs,但是,我们需要几条剪枝: 1.如果只剩下1个或2个同样颜色的方块,那么直接退出 2.相同的块不用交换 3.注意优先性,优先左边换右 ...

  7. Vulnhub 靶场 Os-hackNos WP

    About Os-hackNos 描述 Difficulty : Easy to Intermediate Flag : 2 Flag first user And second root Learn ...

  8. F版本SpringCloud 2—什么是SpringCloud?SpringCloud版本选择

    引言:搭建微服务架构就像是买电脑,使用SpringCloud就是在买品牌机. 前言 昂,美好的天气里,不想直接说技术,给小伙伴萌看看傍晚的天空吧. -- 能找到天上的北极星吗? 上一篇文章中,通过一个 ...

  9. 认识Oracle数据库系统--详细解说

    1.3 认识Oracle数据库系统 Oracle数据库是美国Oracle公司的一款关系型数据库管理系统,简称为Oracle RDBMS,是目前数据库市场上最为强大和流行的数据库系统之一.Oracle是 ...

  10. Asp.Net Core系列 电子书(摘自:Yaopengfei(姚鹏飞))

    链接:https://pan.baidu.com/s/1uSmlArXinvNPKoLvck1hFg 提取码:34ce