PAT A 1033 To Fill or Not to Fill

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​​ (≤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.

知识点:

贪心算法

思路:

针对所处的每一个加油站,有如下情况:

  • 在可以开到的范围内,有更便宜的加油站:

    • 本站加到刚好够开到下一个加油站;下一个加油站是最近的一个比本站便宜的
  • 在可以开到的范围内,没有更便宜的加油站:
    • 本站加满油,下一个加油站是最便宜且,最远的
  • 在可以开到的范围内,没有加油站:
    • 在本站加满油,能开多远开多远
  • 特殊情况:
    • d == 0 花费为0
    • 起点没有加油站 最远走0
 #include <iostream>
#include <algorithm>
using namespace std;
const int maxn = ; double cmax,d,davg;
int n;
struct GSType{
double dist;
double price;
};
struct GSType GS[maxn]; bool cmp(struct GSType a, struct GSType b){
return a.dist<b.dist;
} int main(int argc, char *argv[]) { scanf("%lf %lf %lf %d",&cmax,&d,&davg,&n);
for(int i=;i<n;i++){
scanf("%lf %lf",&GS[i].price,&GS[i].dist);
}
GS[n].dist = d;
GS[n].price = 0.0;
sort(GS, GS+n, cmp); if(d==){ // 特殊情况 起点即终点
printf("0.00\n");
return ;
}
if(GS[].dist!=){ // 特殊情况 起点没有加油站
printf("The maximum travel distance = 0.00\n");
return ;
} //printf("%d\n",GS[0].dist);
double tol_w = 0.0;
double Df = cmax*davg;
double LongestD = 0.0;
double remain = 0.0; int locate = ;
while(locate!=n){
double cheapest = 99999999.9;
int next_locate = -;
bool cheaper = false;
for(int i=locate+;i<=n&&(GS[locate].dist)<GS[i].dist&&GS[i].dist<=(GS[locate].dist+Df);i++){
if(GS[i].price<GS[locate].price){ //有便宜的,找最近的
next_locate = i;
cheaper = true;
break;
}
if(GS[i].price<=cheapest){ // 没有比便宜的,找最便宜的
cheapest = GS[i].price;
next_locate = i;
}
}
if(next_locate == -){ // 没有能到的
LongestD = GS[locate].dist+cmax*davg;
//printf("%f\n",GS[locate].dist);
printf("The maximum travel distance = %.2f\n",LongestD);
return ;
}
if(cheaper){
if((GS[next_locate].dist-GS[locate].dist)/davg>remain){
tol_w += ((GS[next_locate].dist-GS[locate].dist)/davg-remain)*GS[locate].price;
remain = ;
}else{
remain -= (GS[next_locate].dist-GS[locate].dist)/davg;
}
}else{
tol_w += (cmax-remain)*GS[locate].price;
remain = cmax-(GS[next_locate].dist-GS[locate].dist)/davg;
}
locate = next_locate;
}
printf("%.2f\n",tol_w);
}

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

1033 To Fill or Not to Fill的更多相关文章

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

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

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

  4. PAT甲级1033. To Fill or Not to Fill

    PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...

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

  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

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

  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. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

随机推荐

  1. JFinal Web开发学习(五)注册界面和后端验证

    效果: 直接点击注册后 : 后端验证是可靠地,前端js验证是不可靠的.只需要在浏览器删除js验证代码即可突破js验证. 1.注册界面 在WebRoot下新建regist.jsp <%@ page ...

  2. android 开发概述以及相关背景知识

    参考链接:http://www.runoob.com/android/android-architecture.html http://www.runoob.com/android/android-a ...

  3. python消息队列Queue

    实例1:消息队列Queue,不要将文件命名为"queue.py",否则会报异常"ImportError: cannot import name 'Queue'" ...

  4. iOS.Animation.CAMediaTiming

    CAMediaTiming Protocol CALayre 和 CAAnimation 实现了CAMediaTiming 接口. CAMediaTiming 定义了8个属性. speed属性: Co ...

  5. (O)JS核心:call、apply和bind

    1. var func=function(a,b,c){ console.log([a,b,c]); }; func.apply(null,[1,2,3]); //[1,2,3] func.call( ...

  6. Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

    最近在Tomcat上配置一个项目,在点击一个按钮,下载一个文件的时候,老是会报上面的错误.试了很多方法,如对server.xml文件中,增加MaxHttpHeaderSize的大小,改端口,改Tomc ...

  7. vim 中将 TAB 换成 4 个空格

    最近开始学习Python,其语法简单,但是对代码格式要求比较严格.代码采用缩进方式,按照约定采用4个空格的缩进. Linux下配置vim编辑器中Tab键为4个空格方法: 1. 编辑文件: vi /et ...

  8. Laravel Relationship Events

    Laravel Relationship Events is a package by Viacheslav Ostrovskiy that adds extra model relationship ...

  9. [Chrome Headless + Python] 截长图 (Take Full-page Screenshot)

    # -*- coding: utf-8 -*- import time import os from selenium import webdriver from selenium.webdriver ...

  10. TP QQ 微信 微博登录

    use Org\Util\QQconnect; use Org\Util\Wechatauth; use Org\Util\SaeTOAuthV2; use Org\Util\SaeTClientV2 ...