PAT 甲级 1033 To Fill or Not to Fill (25 分) 贪心

电玩女神 2024-02-19 21:51 115阅读 0赞

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 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:

  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

Sample Output 1:

  1. 749.17

Sample Input 2:

  1. 50 1300 12 2
  2. 7.10 0
  3. 7.00 600

Sample Output 2:

  1. The maximum travel distance = 1200.00

这题是看了宝典才有思路的。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. using namespace std;
  5. struct node{
  6. double price;
  7. double dis;
  8. };
  9. struct car{
  10. double account;
  11. double sum;
  12. car()
  13. {
  14. account=0;
  15. sum=0;
  16. }
  17. };
  18. int cmp(node a,node b)
  19. {
  20. return a.dis<b.dis;
  21. }
  22. int main()
  23. {
  24. double count,distance,run;
  25. int n;
  26. car s;
  27. node a[502];
  28. int i,j,k=0;
  29. scanf("%lf %lf %lf %d",&count,&distance,&run,&n);
  30. for(i=0;i<n;i++)
  31. scanf("%lf %lf",&a[i].price,&a[i].dis);
  32. a[n].price=0;
  33. a[n].dis=distance;
  34. sort(a,a+n,cmp);
  35. if(a[0].dis!=0) //如果不是从0开始的, 则不能到达
  36. {
  37. printf("The maximum travel distance = %.2lf\n",0);
  38. }
  39. else
  40. {
  41. k=0;
  42. for(i=k;i<n;)
  43. {
  44. int miner=-1;
  45. double min_p=0x3f3f3f3f;
  46. for(j=i+1;j<=n&&a[j].dis<=a[i].dis+count*run;j++)
  47. {
  48. if(a[j].price<min_p)
  49. {
  50. min_p=a[j].price;
  51. miner=j;
  52. if(min_p<a[i].price) // 如果找到比当前油价更低而且是最近的加气站,就跳出循环
  53. {
  54. break;
  55. }
  56. }
  57. }
  58. if(miner==-1) //如果没找到 符合条件的气站 ,退出循环
  59. break;
  60. double need=(a[miner].dis-a[i].dis)/run*1.0; //算出到下一个气站需要的油量。
  61. if(a[miner].price<a[i].price) // 如果找到比当前油价更低而且是最近的加气站
  62. {
  63. if(s.account<need) // 如果此时油箱的油不够
  64. {
  65. s.sum+=(need-s.account)*a[i].price; // 加到能到目的加气站的油量
  66. s.account=0; // 记得要清零 ,而且顺序别错了
  67. }
  68. else // 如果够的话 ,就减去。
  69. {
  70. s.account-=need;
  71. }
  72. }
  73. else // 如果没找到符合条件的加气站,那就在当前站加满
  74. {
  75. s.sum+=(count-s.account)*a[i].price;
  76. s.account=(count-need);
  77. }
  78. i=miner; // 更新当前加气站
  79. // cout<<a[i].price<<" "<<a[i].dis<<" "<<s.sum<<endl;
  80. }
  81. if(i==n)
  82. printf("%.2lf\n",s.sum);
  83. else
  84. printf("The maximum travel distance = %.2lf\n",a[i].dis+count*run);
  85. }
  86. return 0;
  87. }

发表评论

表情:
评论列表 (有 0 条评论,115人围观)

还没有评论,来说两句吧...

相关阅读