• 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

    题目大意

    给你杭州到某城市的距离ending,假设他们之间是一条直线,然后在这直线上有M个加油站,每个加油站卖的汽油价格都不一定相同。排除所有干扰因素,固定车子每升油可行驶avg距离,一开始车子是木有一滴油的,告诉你ending,M,avg,以及车子油箱的最大容量MAXSIZE,求杭州到某城市所需最少的油费,如果无法抵达,输出最远可行驶的距离。结果保留两位有小数。


    思路

    注意,一开始就没有加油站的情况。然后贪心思想,如果从加油站A出发,在接下来的MAXSIZE*avg距离内,存在价格比A更低的站点B,那我们只需要保证剩下的油可以抵达B即可,否则把在A帮邮箱加满


    C/C++ 

    1. #include
    2. using namespace std;
    3. struct Station{
    4. double price;
    5. int index;
    6. bool friend operator <(const Station &x,const Station &y){
    7. return x.index < y.index;
    8. }
    9. }s[501];
    10. int MAXSIZE,ending,avg,M;
    11. double OP();
    12. int main()
    13. {
    14. cin >> MAXSIZE >> ending >> avg >> M;
    15. for(int z=0;z> s[z].price >> s[z].index;
    16. s[M].index = ending;
    17. sort(s,s+M);
    18. double result = OP(); // 利用 result 的正负判断终点是否能抵达
    19. if(result==0) cout << "The maximum travel distance = 0.00" << endl;
    20. else if(result<0) printf("The maximum travel distance = %.02f",-result);
    21. else printf("%.2f",result);
    22. return 0;
    23. }
    24. double OP(){
    25. if(s[0].index!=0) return 0;
    26. double sum=0,len=0; // 总消费,当前油可行驶记录
    27. int now = 0,maxLen=MAXSIZE*avg; // 当前位置,邮箱最大行驶距离
    28. while (now!=M){
    29. bool ok = false; // 判断从s[now]出发,是否能遇到更便宜油价的站点
    30. int next = now+1,flag=now+1;
    31. if(s[next].index > s[now].index+maxLen) return -(s[now].index + maxLen);
    32. while (s[now].index+maxLen>=s[next].index) {
    33. if (s[next].price <= s[now].price) {
    34. if (len < s[next].index) sum += (s[next].index - len) * 1.0 / avg * s[now].price;
    35. len = s[next].index;
    36. now = next;
    37. ok = true;
    38. break;
    39. } else if (s[next].price < s[flag].price) flag = next;
    40. next++;
    41. }
    42. if(!ok){
    43. sum += (maxLen-len+s[now].index)*1.0/avg * s[now].price;
    44. len = s[now].index + maxLen;
    45. now = flag;
    46. }
    47. }
    48. return sum;
    49. }


     

     

  • 相关阅读:
    【AcWing】3449. 数字根 (数论思维、模拟)
    哈希表、无序集合、映射的原理与实现
    四:ffmpeg参数介绍
    PTA作业笔记——简单的输入输出
    由x-www-form-urlencoded引发的接口对接失败
    vue引入vant框架
    新手想开一个传奇该如何操作?开一个传奇必须掌握哪些知识要点
    力扣(LeetCode)1769. 移动所有球到每个盒子所需的最小操作数(C++)
    Redis集群原理概述
    gitlab-ce实现主备切换集群:rsync+PostgreSQL备份的方式实现快速切换server ip实现伪高可用
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127731863