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

    749.17
    

    Sample Input 2:

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

    Sample Output 2:

    The maximum travel distance = 1200.00

     代码:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. const int inf=0x3f3f3f3f;
    6. struct station{
    7. double price,dis;
    8. };
    9. bool cmp(station a,station b){
    10. return a.dis
    11. }
    12. int main(){
    13. int n,cmax,davg,d;
    14. scanf("%d%d%d%d",&cmax,&d,&davg,&n);
    15. vector sta(n+1);
    16. for(int i=1;i<=n;i++)
    17. scanf("%lf%lf",&sta[i].price,&sta[i].dis);
    18. double maxdis=0,nowdis=0,nowprice,minprice,minpricedis,totalprice=0;
    19. sta[0]={0,d};
    20. sort(sta.begin(),sta.end(),cmp);
    21. if(sta[0].dis!=0){
    22. printf("The maximum travel distance = 0.00\n");
    23. return 0;
    24. }
    25. else
    26. nowprice=sta[0].price;
    27. while(nowdis
    28. int flag=0;
    29. minprice=inf,minprcedis=0;
    30. maxdis=nowdis+cmax*davg;
    31. for(int i=0;i<=n && sta[i].dis<=maxdis;i++){
    32. if(nowdis>=sta[i].dis) continue;//已经走过的站
    33. if(nowprice>sta[i].price){//在当前加油站找到后面可以到达区间比当前站价格要低的加油站
    34. //所以采取的策略是,在本站需要加的油的量 == 当前站到比本站价格低的第一站的距离
    35. totalprice+=nowprice*(sta[i].dis-leftdis-nowdis)/davg;
    36. leftdis=0;
    37. flag=1;
    38. nowdis=sta[i].dis;
    39. nowprice=sta[i].price;
    40. break;
    41. }
    42. if(sta[i].price//记录区间的最低价格的加油站,这是在没有比当前站价格还要低的加油站的情况下采取的策略
    43. minprice=sta[i].price;
    44. minpricedis=sta[i].dis;
    45. }
    46. }
    47. if(flag==0 && minprice!=inf){//如果后面没有比当前站价格还要低的加油站,那就再本站加满油才开始出发
    48. totalprice+=nowprice*(cmax-leftdis/davg);
    49. leftdis=maxdis-minpricedis;
    50. nowdis=minpricedis;
    51. nowprice=minprice;
    52. }
    53. if(flag==0 && minprice==inf){//在可以到达的区间内,已经没有加油站了
    54. printf("The maximum travel distance = %.2lf\n",maxdis);
    55. return 0;
    56. }
    57. }
    58. printf("%.2lf\n",totalprice);
    59. return 0;
    60. }

    好好学习,天天向上!

    我要考研

  • 相关阅读:
    go proto 简单学习
    结构化编程(SP,structured programming)
    Python装饰器的四种定义形式
    Android——编译(二):android.mk的相关知识
    去除织梦dedecms sp1与sp2版本上传图片大小超过2MB的限制
    代码随想录30——回溯:332重新安排行程、51N皇后、37解数独
    RocketMQ(3)之事务消息
    IS ATTENTION BETTER THAN MATRIX DECOMPOSITION
    【腾讯云 HAI域探秘】高性能服务器引领AI革新浪潮:从AI绘画、知识问答到PyTorch图像分类、视频检测的全方位探索
    对象创造模式
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/127678943