首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

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

  • 24-03-03 00:43
  • 3878
  • 5098
blog.csdn.net

点击链接PAT甲级-AC全解汇总

题目:
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.

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Sample Output 1:

749.17
  • 1

Sample Input 2:

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

Sample Output 2:

The maximum travel distance = 1200.00
  • 1

题意:
从杭州出发到1300远的地方,假设路程是一条直线,给你每个加油站的价格和到杭州的距离,计算最小的油费。
题目的例子没有给出计算的过程,只显示了一个油价,其实是这么算的:
150.0/127.1+150.0/127+600.0/126.85+300.0/127+50.0/127.3+50.0/126 = 749.167 保留两位四舍五入749.17

注意,满油箱能跑600KM

  • 【在0km处,油箱0/600】600km范围内,最便宜的加油站是300km处的6.85
    所以在0处 加300km的油,到300km处,油箱空;

  • 【到300km处,油箱0/600】此时600km范围内[300,900],最便宜的加油站是自己6.85,
    所以在300km处加600km的油,加满;
    因为不到1300km,需要补油,第二便宜的是600km处的7.00,先到600km处,油箱还剩300km;

  • 【到600km处,油箱300/600】此时600km范围内[600,1200],最便宜的加油站是自己7.00;
    所以在600km处加300km的油,加满;
    因为不到1300km,需要补油,第二便宜的是1000km处的7.30,先到1000km处,油箱还能跑200km;

  • 【到1000km处,油箱200/600】此时600km范围内[1000,1600],最便宜的是1250km处的6.00,
    所以在1000km处加50km的油,能跑到1250km处,油箱空;

  • 【到1250km处,油箱0/600】此时还剩50km,所以加50km的油。

我的思路:
找到从当前位置max_dis内下一个比自己便宜的加油站next

  • 如果找到,则加油到刚好可以跑到next
  • 如果没有,则加满,跑到最后一个加油站

注意oil_sum始终不要超过distance
注意: 并不是到范围内最便宜的加油站,只要下一个比当前便宜就去下一个补油!!!

注意: case2的起点没有加油站

注意: 最远到达的距离 这个距离不一定是整数 因为可能正好加油的地方不是pre的整数倍,所以求距离的时候要double一下
在这里插入图片描述
在这里插入图片描述

还有,有人说有的加油站在同一个距离,价格不一样,我删了这个min(),不影响ac,说明价格都一样的。

我的代码:

#include
using namespace std;

int main()
{
    int volume,distance,pre,N;
    double price[30010]={0};
    bool flag[30010]={false};
    cin>>volume>>distance>>pre>>N;
    for(int i=0;i<N;i++)
    {
        int s;
        double p;
        cin>>p>>s;
        price[s]=p;
    }

    if(!price[0])
    {
        printf("The maximum travel distance = 0.00");
        return 0;
    }

    int max_dis=volume*pre;//满油的最远距离(km)
    int pos=0;//当前的位置(km)
    int oil_now=0;//当前的油量(km)
    double p_sum=0;//累计的花费

    while(pos<distance)
    {
        //找max_dis内下一个比自己便宜的加油站next
        int next_pos=pos,last=pos;
        for(int i=pos;i<=pos+max_dis;i++)
        {
            if(!price[i])continue;//这里没有加油站
            last=i;//最后一个加油站
            if(price[i]<price[pos])//第一个比pos便宜的加油站
            {
                next_pos=i;
                break;
            }
        }

        if(last==pos&&pos+max_dis<distance)//没有其他加油站了
        {
            printf("The maximum travel distance = %.2f\n",double(pos+max_dis));
            return 0;
        }

        int add_v;//记录这次打算加油的量
        //如果找到,则加油到刚好可以跑到next
        if(next_pos!=pos)add_v=next_pos-pos-oil_now;//距离-余量
        else//如果没有,则加满,跑到最后一个加油站
        {
            add_v=max_dis-oil_now;     //最远-余量
            next_pos=(last==pos)?(pos+max_dis):last;//范围内没有加油站
        }

        if(next_pos>=distance)add_v=distance-pos-oil_now;//不超过终点

        p_sum+=1.0*add_v*price[pos]/pre;
        oil_now=oil_now+add_v-(next_pos-pos);
        pos=next_pos;
    }
    printf("%.2f\n",p_sum);

    return 0;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
注:本文转载自blog.csdn.net的邂逅模拟卷的文章"https://blog.csdn.net/qq_34451909/article/details/105414452"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top