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. @params、@PathVariabl和@RequestParam用法与区别

    [1]params params: 指定request中必须包含某些参数值是,才让该方法处理. @RequestMapping(value = "testParamsAndHeaders&q ...

  2. echart.js组件编写

    1.传参 <area-chart :chartdata='monitorTimes' :datatype='typeSelected' :dataX = '"tid"' :t ...

  3. Vue v-if 和 v-show

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 偷懒把本来要判断输入值的textbox 输出提示值,结果点两次程序异常

    调试程序是苦并快乐着的事, 为防止用户直接点击导入下表,于是我设置提示,点击时先判断textbox 里边有无值,为空的话也在textbox做提示 程序如下: 点一次可以正常输出到textbox中,点第 ...

  5. Java图形化界面设计——GridBagConstraints

    JAVA布局模式:GridBagConstraints终极技巧参数详解 布局模式 :GridBagConstraints布局,先发一个实例: gridx = 2; // X2 gridy = 0; / ...

  6. Liunx cal

    1.命令格式: cal [参数][月份][年份] 2.命令功能: 用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份 3.命令参数: -1 显示一个月的 ...

  7. Jenkins2.0中的pipeline

    jenkins的实现是标准的master/slave模式,用户与master交互,master将job分布到slave上运行. jenkins的基本概念: 1. master, 也就是jenkins的 ...

  8. 如何实现HashMap的同步

    HashMap可以通过Map m = Collections.synchronizedMap(new HashMap())来达到同步的效果.具体而言,该方法会返回一个同步的Map,该Map封装了底层的 ...

  9. 【UI测试】--快捷键组合

  10. SQL表两列取一列唯一值的记录

    问下SQL表两列取一列唯一值的 A列         B列       C列 1001      AA      2012-01-02 1001      BB      2012-02-05 100 ...