PAT-AL 1033. To Fill or Not to Fill

【PAT 1033】 To Fill or Not to Fill 贪心算法&模拟 1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Guochuan With highways available, drivi 阅读详情

1、知识点:贪心
2、思路:先将站按照距离从小到大排序。假设我有N个桶,第i号桶装第i站的汽油(i从0开始),第0站加满,到第i站时,选择从第i-1站到达此站最便宜的汽油(如果最便宜的不够用,选择次便宜的),然后将N个桶中所有比此站更贵的汽油原价卖出(说明之前买的多了,够用就好),然后加此站的油至加满(不管是不是比之前或后面的更贵,因为不加满怕到不了下个站)。最后到达终点时将所有剩下的油原价卖出即可。

3、注意:①如果第0号站的初始距离不是0,说明最大距离就是0;
②对于第i站,还要判断能否从第i-1站到达此站;
③有的站距离可能超过终点。

/*用途:
**说明:
**算法:
*/

//#define LOCAL
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXPRICE 2147483647   //最大油价
#define MAXN 500+10
struct station{
    double price;   //油价
    double dis;     //距离原点
};

struct mybox{       //桶结构
    double price;   //油价
    double vol;     //油量
};
double Cmax, D, Davg;
int N;
station sta[MAXN];
vector<mybox> box;  //桶向量
double money;       //最终花费

bool cmp_dis(station a, station b);   //按距离从小到大排序
bool can_arrive(int n);   //判断能否到达第n站
double get_far(int n);    //最终能走多远
void clear_and_plus(int n);   //用掉便宜油,加上此站油
void add_box(mybox mb);   //每到一个站,添加一个桶
void update(int n, double &plu);  //维护桶
void repay();   //卖出剩下的油
int main()
{
#ifdef LOCAL
    freopen(".in", "r", stdin);
    freopen(".out", "w", stdout);
#endif

    scanf("%lf%lf%lf%d", &Cmax, &D, &Davg, &N);
    for(int i=0; i<N; i++)
        scanf("%lf%lf", &(sta[i].price), &(sta[i].dis));

    sort(sta, sta+N, cmp_dis);
    if(sta[0].dis){   //如果0号站距离不为0
        printf("The maximum travel distance = 0.00");
            return 0;
    }

    sta[N].price = MAXPRICE;   //假设终点站的油天价
    sta[N].dis = D;

    mybox mb;    //起点站对应的桶
    mb.price = sta[0].price;   
    mb.vol = Cmax;
    box.push_back(mb); 
    money += mb.price * Cmax;   

    for(int i=1; i<N+1; i++){
        if(!can_arrive(i)){   //如果不能到达此站
            printf("The maximum travel distance = %.2lf", get_far(i));
            return 0;
        }
        else if(sta[i].dis >= D){   //此站已超过或就是终点
            clear_and_plus(i);
            repay();
            printf("%.2lf", money);
            return 0;
        }
        clear_and_plus(i);
    }

    return 0;
}

bool cmp_dis(station a, station b)
{
    return a.dis < b.dis;
}

bool can_arrive(int n)
{
    return Cmax * Davg >= sta[n].dis - sta[n-1].dis;
}

double get_far(int n)
{
    return sta[n-1].dis + Davg*Cmax;
}

void clear_and_plus(int n)
{
    double need, plus;   //从第n-1站到第n站所需油
    plus = need = (sta[n].dis - sta[n-1].dis) / Davg;
    int len = box.size();
    for(int i=0; i<len && need>0; i++){   //桶已经按照油价升序排列
        if(box.at(i).vol >= need){   //该桶油够用
            box.at(i).vol -= need;
            break;
        }
        else{   //该桶油不够用
            need -= box.at(i).vol;
            box.at(i).vol = 0;
        }
    }   
    update(n, plus);   //维护桶向量(注意:plus引用传递)

    mybox mb;
    mb.price = sta[n].price;
    mb.vol = plus;
    add_box(mb);        //加桶
    money += sta[n].price * plus;   //加上此站油钱
}

void add_box(mybox mb)
{
    int len = box.size();
    for(int i=0; i<len; i++){
        if(mb.price < box.at(i).price){   //将新添加的桶按照价格升序插入
            box.insert(box.begin()+i, mb);
            return;
        }
    }
    box.push_back(mb);
}

void update(int n, double &plu)
{
    for(int i=0; i<box.size(); i++)
        if(box.at(i).vol == 0 || box.at(i).price > sta[n].price){   //如果是空桶或者该桶油价比第n站贵,原价卖掉
            plu += box.at(i).vol;
            money -= box.at(i).price * box.at(i).vol;
            box.erase(box.begin() + i);
            i--;
        }
}

void repay()
{
    int len = box.size();
    for(int i=0; i<len; i++)
        money -= box.at(i).price * box.at(i).vol;
}
1033 To Fill or Not to Fill (PAT Advanced) 坑点 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 ga... 阅读详情

相关推荐

1014. 福尔摩斯的约会 (20)-PAT乙级真题

1014. 福尔摩斯的约会 (20) 大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧!3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间“星期四 14:04”,因为前面两字符串中第1对相同的大写英文字母(大小写有区分)是第4个字母’D’,代表星期四;第2对相同的字符是’

柳婼 の blog 2646

PTA-甲级 1033 To Fill or Not to Fill(测试点2)

PTA-甲级 1033 To Fill or Not to Fill(测试点2) 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 g

study_cs的博客 705

RS485/CAN总线防护电路设计:TVS选型与ESD防护实战解析

本文深入解析了RS485与CAN总线防护电路设计中的TVS选型与ESD防护实战技巧。针对工业现场常见的静电放电、浪涌等干扰,文章详细阐述了TVS管关键参数的选择、多级防护体系的构建,并特别强调了在隔离电路设计中地回路处理的重要性,帮助工程师设计出更可靠的通信接口。

metaverse5vr的博客 389

PAT甲级 1033 To Fill or Not to Fill (测试点2、4)

PAT甲级1033题

blueblue0808的博客 942

特征选择(模型输入参数的分析选择)方法汇总

特征选择是模式识别和机器学习领域的重要研究课题之一。为了提高准确率, 人们往往最大限度地提取特征信息。然而, 过大的特征向量维数不仅导致计算成本的增加, 复杂运算也随之带来分类识别率的下降。因此, 通过合适的特征选择算法, 去除无关特征和冗余特征, 获得有助于分类的最优特征子集, 对提高识别性能和降低计算成本具有重要意义。 特征选择主要有两个功能: 减少特征数量、降维,使模型泛化能力更强,减少过拟合 增强对特征和特征值之间的理解 参考资料: 优快云《几种常用的特征选择方法》 知乎《机器学习中,.

David's Tweet 1万+

Word公式一键转MathType保姆级教程(附omml2mml.xsl报错终极解决方案)

本文提供Word公式一键转MathType的保姆级教程,重点解决常见的omml2mml.xsl报错问题。通过详细分析报错原因、官方解决方案实操步骤及转换后格式优化技巧,帮助用户高效完成公式转换,适用于科研论文和商业文档编辑。

weixin_30082367的博客 555

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 tim...

weixin_30371469的博客 521

MATLAB代码实现了一个柱塞泵配流盘过流面积的计算和可视化功能

该MATLAB代码实现了一个柱塞泵配流盘过流面积的计算和可视化功能。输出曲线可直接用于验证设计的合理性(如是否存在过流面积突变或不足)。的不同范围,选择对应的几何模型计算过流面积。通过分段逻辑计算柱塞腔在不同旋转角度(过流面积在特定条件下会被限制(如。),确保结果符合物理实际。条件分支,根据旋转角度。

huanghm88的专栏 582

PAT甲级 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. Dif

changeforeve的博客 207

智能驾驶域控制器的“终极大考”:解析EOL测试的核心价值与技术实践

在智能驾驶技术高速发展的今天,域控制器(Domain Controller)作为车辆“大脑”的核心部件,承担着传感器数据处理、决策规划、执行控制等关键任务。然而,这个集成了数十个芯片、运行着数百万行代码的复杂系统,如何确保其出厂前的功能安全与性能稳定?答案就藏在被称为“生产线最后一道防线”的EOL(End of Line)测试中。

汽车电子软件领域知识分享 704

XCP实战系列介绍07-使用ASAP2 Editor生成A2l文件详解

介绍通过ASAP2 Editor工具生成A2l文件步骤详解

initiallizer的博客 7298

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

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

宝付支的博客 392

用 ABAP ADK 写一个可控的自建表归档程序:从选择屏幕到 ARCHIVE_SAVE_OBJECT 的完整链路

文章摘要: 本文介绍了SAP系统中数据归档的核心概念与实践方法。归档旨在将不常访问但需保留的数据从在线数据库移出,以减轻系统负载,同时满足合规要求。文章重点阐述了: 归档与备份、重组的本质区别 SAP归档框架的核心组件:归档对象(AOBJ)、归档管理界面(SARA)和ADK开发工具包 通过YAPTMAIN表示例,展示了完整的归档流程: 使用选择屏幕控制归档范围 管理归档会话 写入归档记录 生成统计报表 强调了归档程序设计中需要考虑的运维、审计和性能因素 该方案可有效解决自建日志表"悄悄长胖&quo

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 229

1033.To Fill or Not to Fill (25)

1033.To Fill or Not to Fill (25)pat-al-10332017-02-05 贪心 在起始处没有加油站所以一步也走不了的情况特殊处理一下 分三种情况,一是油箱加满也到不了下一个加油站(直接print能到的最远处然后return 0)、二是能走到的距离内有比当前站油价低的(就加够可以行驶到那一站的油就可以了)、三是能走到的距离内有加油站但没有比当前油价低的(那就加满油,行

一剑钦安 213

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

*1033. To Fill or Not to Fill

不加target==n这个条件测试点1会出现超时=皿=没找到原因。 很有价值的贪心题目!#include #define INFINITY 10000 /*有时候不用加满油,只要加够向终点的油即可*/ typedef struct Station { float price; float dist; }Station; Station GasS[501]; int FindTarget(i

你知道小鱼干藏在哪里了喵 436

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

1033. To Fill or Not to Fill 解析

这个题这位大神写的太好了。太详细了。 http://www.cnblogs.com/XBWer/p/3866486.html

sheepyWYY的博客 777

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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

解题过程的小记录,如有错误欢迎指出。 难度:四星 小导航~题目分析注意点我的解题过程思路bug代码dalao的代码借鉴点 题目分析 注意点 我的解题过程 思路 bug 一开始毫无思路,无从着手 代码 dalao的代码 全部代码因版权原因不放出来,大家可以自行去柳神博客购买或者参考晴神的上机笔记~ 借鉴点 ...

SpicyChicken的自我修养 350

PAT A1033 To Fill or Not to Fill (25 分) 贪心 模拟

题目大意:给出N个加油站的油费以及距离出发点的距离,汽车油箱有容量上限,求出从出发点到达目的地的最少油费。如果不能到达目的地,输出最远能到达的距离。 个人觉得是相当复杂的一个模拟题,单步跟踪了好几次逻辑才写完整,考察的主要是贪心策略。要点列举如下: ① 目的地也作为一个加油站,油费为0,这样能保证在查找最低油费的加油站时一定能查找到目的地。 ② 在当前所在的加油站往后看,在能...

HackingGate的博客 303

PATA 1033 To Fill or Not to Fill(25 分)解题报告

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

热心市民小黎的博客 2340

[Python](PAT)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 ga...

贰拾壹 395

PAT 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 ga...

phil的博客 249

PAT甲级A1033 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 ga...

vir_lee的博客 319

1033 To Fill or Not to Fill (25分)

个人认为这是比较难的一个动态规划题目了,我做了好几次才写出来。 核心思想就是从0点开始考虑,寻找在他能行驶的最大范围内寻找一个价格最低的加油站,然后讨论从当前点能否过去,以及该点油价是否小于当前点。然后分情况讨论。 附本人AC代码: #include<iostream> #include<vector> #include<map> #include<math.h> using namespace std; struct Node { int add, val

ZiYuTongXue的博客 1138

1033. To Fill or Not to Fill (25)

贪婪(设装满油可跑的最大距离为s_max) 每到一个加油点的三种情况: 1.s_max内没有加油点,那么加满油跑s_max结束 2.s_max内有加油点,且有比当前加油点价格低的(选择最近的一个),则加油,车跑到那个加油点恰好没油 3.s_max内有加油点,但没有比当前加油点价格低的,那么选择价格最低的一个,加满油,跑到那个加油点 4.递归(1.2.3),提前

Bendaai的博客 339

1033 To Fill or Not to Fill (25 分)测试点0、1

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 di

weixin_45050148的博客 404

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 ga...

haohao0626的博客 573
上一篇: PAT-AL 1025. PAT Ranking
下一篇: URAL-1101. Robot in the Field
EverydayIsLife
博客等级 码龄12年 9粉丝 20原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值