题目描写叙述:

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.

输入:

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.

输出:

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.

例子输入:
  1. 50 1300 12 8
  2. 6.00 1250
  3. 7.00 600
  4. 7.00 150
  5. 7.10 0
  6. 7.20 200
  7. 7.50 400
  8. 7.30 1000
  9. 6.85 300
  10. 50 1300 12 2
  11. 7.10 0
  12. 7.00 600
例子输出:
  1. 749.17
  2. The maximum travel distance = 1200.00
来源:

2012年浙江大学计算机及软件project研究生机试真题

这道题确实挺难的,花了好久的时间,然后自己考虑不全面,最后參考别人的代码才搞定。

就不敢写原创了。。。



http://ziliao1.com/Article/Show/73A96AF77079A6C32C4AA82604FCF691



典型的贪心法。

思想就是考虑下一次在何网站加油(从而决定了在本网站须要加多少油)。

考虑这样几种情况:

1、到达下一网站所需油量 > 油箱最大容量:则下一网站不可达。做法是把油箱加满,尽可能跑,然后break掉。

2、下一网站可达,且油价比本网站廉价:则应尽早“换用”更廉价的油。做法是本站加够就可以。使得刚好能到达下一站。

3、下一网站可达。但油价比本网站贵:此处第一次做错了,不应该在本站把油箱加满,而应该继续寻找满油的条件下可达的下一个比本站廉价的网站。若找到,则加够就可以(所以情况2能够并到这里);若未找到,则在本站将油箱加满。

  1. #include <algorithm>
  2.  
  3. #include <iomanip>
  4. #include <iostream>
  5. using namespace std;
  6. struct station
  7. {
  8. float price;
  9. float dist;
  10. };
  11. station st[501];
  12. float cmax, d, davg;
  13. int n;
  14. bool cmp(station a, station b)
  15. {
  16. return a.dist < b.dist;
  17. }
  18. // 寻找下一个可达的廉价网站
  19. int nextCheaper(int now)
  20. {
  21. for(int i = now; i < n; i++)
  22. {
  23. if(st[i].dist - st[now].dist > cmax * davg) break;
  24. if(st[i].price < st[now].price)
  25. return i;
  26. }
  27. return -1;
  28. }
  29. int main()
  30. {
  31. while(cin >> cmax)
  32. {
  33. cin >> d >> davg >> n;
  34. for(int i = 0; i < n; i++)
  35. cin >> st[i].price >> st[i].dist;
  36. st[n].price = -1; st[n].dist = d;
  37. n = n + 1;
  38. sort(st, st + n, cmp);
  39. int nowst = 0;
  40. float nowgas = 0;
  41. float cost = 0;
  42. while(nowst < n - 1)
  43. {
  44. if(nowst == 0 && st[0].dist != 0)
  45. {
  46. st[nowst].dist = 0; break;
  47. }
  48. float needgas = (st[nowst + 1].dist - st[nowst].dist) / davg;
  49. if(needgas > cmax)
  50. {
  51. float addgas = cmax - nowgas;
  52. cost += addgas * st[nowst].price;
  53. st[nowst].dist += cmax * davg;
  54. break;
  55. }
  56. int nextc = nextCheaper(nowst);
  57. if(nextc == -1)
  58. {
  59. float addgas = cmax - nowgas;
  60. nowgas = cmax;
  61. cost += addgas * st[nowst].price;
  62. nowgas -= needgas;
  63. nowst = nowst + 1;
  64. }else{
  65. needgas = (st[nextc].dist - st[nowst].dist) / davg;
  66. float addgas = needgas - nowgas;
  67. if(addgas > 0)
  68. {
  69. nowgas += addgas;
  70. cost += addgas * st[nowst].price;
  71. }
  72. nowgas -= needgas;
  73. nowst = nextc;
  74. }
  75. }
  76. if(nowst == n - 1)
  77. cout << fixed << setprecision(2) << cost << endl;
  78. else{
  79. float maxdist = st[nowst].dist;
  80. cout << "The maximum travel distance = "<< fixed << setprecision(2) << maxdist << endl;
  81. }
  82. }
  83. return 0;
  84. }

以下是我模仿大神自己手写的代码,差点儿都改动的全然一样了。。。眼下还是过不了。郁闷

有时间再研究一下啦。。。如今 真的发现不了什么错误了

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5. struct station
  6. {
  7. float pri;
  8. float dis;
  9. }a[999];
  10. int c,d,davg,n;
  11. int cmp(station t1,station t2)
  12. {
  13. return t1.dis<t2.dis;
  14. }
  15. int next(int now)
  16. {
  17. for(int i=now+1;i<=n&&a[i].dis-a[now].dis<=c*davg;i++)
  18. {
  19. if(a[i].pri<a[now].pri)
  20. return i;
  21. }
  22. return -1;
  23. }
  24.  
  25. int main()
  26. {
  27. int i;
  28. while(cin>>c>>d>>davg>>n)
  29. {
  30. for(i=0;i<n;i++)
  31. {
  32. scanf("%f %f",&a[i].pri,&a[i].dis);
  33. }
  34. a[n].pri=-1;
  35. a[n].dis=d;
  36. sort(a,a+n,cmp);
  37. // for(i=0;i<n;i++)
  38. //printf("%f %f\n",a[i].dis,a[i].pri);
  39. int nowst=0;
  40. float anspri=0,ansdis=0,nowgas=0;
  41. while(nowst<n)
  42. {
  43. if(nowst==0&&a[0].dis!=0)
  44. {
  45. ansdis=0;
  46. break;
  47. }
  48. float needgas=(a[nowst+1].dis-a[nowst].dis)/davg;
  49. if(needgas>c)
  50. {
  51. ansdis+=davg*c;
  52. break;
  53. }
  54. int nextst=next(nowst);
  55. // printf("%d %d %f\n",nowst,nextst,anspri);
  56. if(nextst==-1)
  57. {
  58. anspri+=(c-nowgas)*a[nowst].pri;
  59. nowgas=c-needgas;
  60. ansdis=a[nowst+1].dis;
  61. nowst+=1;
  62.  
  63. }
  64. else
  65. {
  66. float addgas=(a[nextst].dis-a[nowst].dis)/davg-nowgas;
  67. if(addgas>0)
  68. {
  69. nowgas=0;
  70. anspri+=addgas*a[nowst].pri;
  71. }
  72. else
  73. nowgas-=needgas;
  74.  
  75. nowst=nextst;
  76. ansdis=a[nowst].dis;
  77.  
  78. }
  79. }
  80. if(nowst==n)
  81. printf("%.2f\n",anspri);
  82. else
  83. printf("The maximum travel distance = %.2f\n",ansdis);
  84. }
  85. return 0;
  86. }
  87.  
  88. /**************************************************************
  89. Problem: 1437
  90. User: HCA1101
  91. Language: C++
  92. Result: Wrong Answer
  93. ****************************************************************/

九度OJ #1437 To Fill or Not to Fil的更多相关文章

  1. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

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

  2. 九度OJ 1437 To Fill or Not to Fill -- 贪心算法

    题目地址:http://ac.jobdu.com/problem.php?pid=1437 题目描述: With highways available, driving a car from Hang ...

  3. 九度OJ 1437 To Fill or Not to Fill

    题目大意:小明从杭州去往某目的地,要经过一些加油站,每个加油站的价格不一样.若能顺利到达,求加油费用最少为多少,否则求出能行驶的最远距离. 思路:贪心算法 1>若下一加油站的价格更便宜,则只需走 ...

  4. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  5. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  6. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  7. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  8. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  9. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

随机推荐

  1. shell-code-exerciese-1

    &&&&&&&&&&&&&&&&&&&& ...

  2. cs229_part4

    又到了一节很重要的课,因为这个学习理论是从统计角度为机器学习算法提供了一个理论基础. 学习理论 问题背景 先回顾一下我们第一节课提到的机器学习的组成: 第一节课只是简单的提了一下,现在我们要真正来分析 ...

  3. Linux下二进制文件安装MySQL

    MySQL 下载地址:https://dev.mysql.com/downloads/mysql/ 并按如下方式选择来下载安装包. 1. 设置配置文件/etc/my.cnmore /etc/my.cn ...

  4. 00030_ArrayList集合

    1.数组可以保存多个元素,但在某些情况下无法确定到底要保存多少个元素,此时数组将不再适用,因为数组的长度不可变 2.JDK中提供了一系列特殊的类,这些类可以存储任意类型的元素,并且长度可变,统称为集合 ...

  5. Linux下文件打包与解包

    打包(.tar):  tar -cvf Pro.tar /home/lin/Pro   #将/home/lin/Pro文件夹下的所有文件打包成Pro.tar 打解包(.tar.gz)  tar -cv ...

  6. Leetcode 397.整数替换

    整数替换 给定一个正整数 n,你可以做如下操作: 1. 如果 n 是偶数,则用 n / 2替换 n.2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n.n 变为 1 所需的最小替换次数是 ...

  7. 使用ANT将Android打包成Jar包

    本文主要实现使用ANT,将Android项目打包成jar,为方便其他项目使用. ANT可以去官网下载(http://ant.apache.org/) 先介绍打包的步骤,打包脚本下方贴出 步骤: 1,将 ...

  8. P1133 教主的花园 (动态规划)

    题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢 3种树,这3种 ...

  9. HashMap构造函数有哪些

    hashMap有4个构造函数: public HashMap(int initialCapacity, float loadFactor) public HashMap(int initialCapa ...

  10. poj1930 数论

    Dead Fraction Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1258   Accepted: 379 Desc ...