1033. To Fill or Not to Fill (25)

时间:2022-09-04 18:05:37

 题目链接:http://www.patest.cn/contests/pat-a-practise/1033

题目:

1033. To Fill or Not to Fill (25)

时间限制
10 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

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 N lines 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

分析:

把不能到达和能到达分开讨论,特别要注意不能到达分为(1),中间距离大于满邮箱的行驶距离(2),起点没有加油站。

在其余的情况的都是能到达终点的,那么选择油费最小的策略就是:在当前加油站到满油箱行驶距离中的加油站中,假设有油价比当前油价小的S加油站,则仅仅加油到满足S加油站的地方。然后从S加油站继续寻找。假设没有找到比当前油站油价小的。则在当前加油站加满油。行驶到下一油站继续寻找。

AC代码:

#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
struct Station{
double price;
double location;
bool operator <(const Station &A)const{
return location < A.location;
}
};
vector<Station>V;
int main(void){
//freopen("F://Temp/input.txt", "r", stdin);
int C, D, dis_per, N;
scanf("%d%d%d%d", &C, &D, &dis_per, &N);
for (int i = 0; i < N; i++){
Station tmp;
scanf("%lf %lf", &tmp.price, &tmp.location);
V.push_back(tmp);
}
Station tmp;
tmp.location = D;
tmp.price = 0;
V.push_back(tmp);//join the Destination to the Station ,As the price = 0;
sort(V.begin(), V.end());
double distance1time = C * dis_per;//满油箱行驶的距离
for (int i = 0; i <V.size() - 1; i++){
if (V[0].location > 0){//假设起点没有加油站,则最远距离就是0。这是一个測试点
printf("The maximum travel distance = 0.00\n");
return 0;
}
if (V[i + 1].location - V[i].location > distance1time){//假设两个加油站的距离大于满油箱的距离
double max;
max = V[i].location + distance1time;
printf("The maximum travel distance = %.2lf\n", max);
return 0;
}
}//The situation that the car can't reach the Destination.
double remain, money, distance;
remain = 0; money = 0; distance = 0;
int now_Station = 0, next_Station;
while (distance < D){
next_Station = -1;
for (int i = now_Station + 1; i < V.size(); i++){//寻找到下一个比当前加油站油价小的加油站
if (V[i].location - V[now_Station].location >distance1time)break;
else if (V[i].price <= V[now_Station].price){
next_Station = i;
break;
}
}
if (next_Station != -1){//找到了更廉价的加油站
double dis_tmp = V[next_Station].location - V[now_Station].location;
distance = V[next_Station].location;
if (remain > dis_tmp / dis_per){//假设当前的油够开到那个更廉价的加油站,则不用加油
remain = remain - dis_tmp / dis_per;
money = money;
}//have enough gas
else{//假设油不够开到更廉价的加油站。则加到能刚好开到那个加油站的油就好
money += (dis_tmp / dis_per - remain)* V[now_Station].price;
remain = 0;
}//gas is not enough
now_Station = next_Station;
}//find the cheaper station
else{//假设没有找到,即当前加油站是能行驶到的路程中油价最小的加油站
double dis_tmp = V[now_Station + 1].location - V[now_Station].location;
distance = V[now_Station + 1].location;
money += (C - remain) * V[now_Station].price;//那么就把油箱加满
remain = C - dis_tmp / dis_per;
now_Station++;
}//can't find the cheaper one ,so full the tank;
}//while
printf("%.2lf\n", money);
return 0;
}

截图:

1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25)

——Apie陈小旭

1033. To Fill or Not to Fill (25)的更多相关文章

  1. 【贪心】PAT 1033&period; To Fill or Not to Fill &lpar;25&rpar;

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

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

  3. PAT 甲级 1033 To Fill or Not to Fill &lpar;25 分&rpar;(贪心&comma;误以为动态规划&comma;忽视了油量问题)&ast;

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

  4. PAT 1033 To Fill or Not to Fill&lbrack;dp&rsqb;

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

  5. pat1033&period; To Fill or Not to Fill &lpar;25&rpar;

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

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

  7. PAT甲级1033&period; To Fill or Not to Fill

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

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

  9. PAT&lowbar;A1033&num;To Fill or Not to Fill

    Source: PAT A1033 To Fill or Not to Fill (25 分) Description: With highways available, driving a car ...

随机推荐

  1. Android中如何使用命令行查看内嵌数据库SQLite3

    转载博客:http://www.linuxidc.com/Linux/2011-06/37135.htm 在上图中,除了最后一个红色的方框,其它方框都是adb shell下的命令. [1]在Andro ...

  2. jQuery常用的元素查找方法总结

    $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("di ...

  3. AspectFill VS&period; AspectFit

    从去年10月进入公司,到现在差不多忙碌了3个月,期间几乎所有精力和时间都花在了公司的项目上,有很多工作学习的心得一直没有总结,趁周末无事就来使这写一写. 除了刚进公司的那一个月是做一些修修补补的工作, ...

  4. 使用ContentResolver添加数据、查询数据

    import java.util.ArrayList;import java.util.HashMap;import java.util.Map; import android.os.Bundle;i ...

  5. APNs-远程推送

    一.开发iOS程序的推送功能, iOS端需要做的事 1.请求苹果获得deviceToken 2.得到苹果返回的deviceToken 3.发送deviceToken给公司的服务器 4.监听用户对通知的 ...

  6. CSAPP(深入理解计算机系统)读后感

    9月到10月8号,包括国庆七天,大概每天5小时以上的时间,把Computer System: A Programmer Perspective 2rd version(深入理解计算机系统)的英文版啃完 ...

  7. Struts2【开发Action】知识要点

    前言 前面Struts博文基本把Struts的配置信息讲解完了.....本博文主要讲解Struts对数据的处理 Action开发的三种方式 在第一次我们写开发步骤的时候,我们写的Action是继承着A ...

  8. Spring Security Oauth2 的配置

    使用oauth2保护你的应用,可以分为简易的分为三个步骤 配置资源服务器 配置认证服务器 配置spring security 前两点是oauth2的主体内容,但前面我已经描述过了,spring sec ...

  9. io&period;undertow&period;websockets&period;jsr&period;ServerWebSocketContainer cannot be cast to org&period;apache&period;tomcat&period;websocket&period;server&period;WsServerContainer

    Caused by: java.lang.ClassCastException: io.undertow.websockets.jsr.ServerWebSocketContainer cannot ...

  10. Git回滚代码暴力法

    Git回滚有多种方式,这里使用的是[强制提交到远程分支] 效果为:如回滚前的提交记录是 1.2.3.4,使用这种方法回滚到2,那么提交记录就变成了1.2. 操作方法: 需要在本地的Git仓库,右键选择 ...