pat-advanced-1033

2024-01-13 19:58
文章标签 pat advanced 1033

本文主要是介绍pat-advanced-1033,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1033. To Fill or Not to Fill (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

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:
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
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:
The maximum travel distance = 1200.00
贪心的一道好题,题目样例给得很好,几乎把坑点暗示出来了

思路:

1.若起点无加油站,则不能出发(注意距离也要用浮点数表示,注意eps的使用)

2.若可以出发,则从该点开始查找在可达范围内的价格最低的加油站(在我看别人的解题思路时,一个分层油箱的概念挺有意思的,即油箱中有不同价格的油,但是使用时优先使用最便宜的油,可以用优先队列实现)

3.价格最低的加油站分为两种:(1)价格小于当前加油站并且是价格小于当前加油站中的离当前位置最近的 (2)价格大于当前加油站的它是当前加油站可达范围内最便宜的

4.根据这两种情况确定贪心策略:(1)首先在起点把油箱装满 (2)对于第一种加油站一旦遇到就先把剩余的油抛弃掉再把油箱装满 (3)对于第二种加油站,把油箱装满但不要把以前更便宜的油扔掉

5.当到达下一个点时,才计算上一个点的消耗,可以把终点当做一个价格为0加油站放入数组中,需要对数组排序

#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<queue>
#define eps 1e-8
using namespace std;
struct list
{double price;double pos;
};
struct petrol
{double vol;double price;petrol() {	}petrol(double c,double d){vol=c;price=d;}friend bool operator < (struct petrol a,struct petrol b){return a.price>b.price;}
}; 
bool cmp(struct list a,struct list b)
{if(fabs(a.pos-b.pos)<=eps)return a.price<b.price;elsereturn a.pos<b.pos;
}
int main()
{double c,d,p,maxd;int n;struct list an[505];scanf("%lf%lf%lf%d",&c,&d,&p,&n);for(int i=0;i<n;i++){scanf("%lf%lf",&an[i].price,&an[i].pos);}an[n].price=0;an[n].pos=d;sort(an,an+n+1,cmp);if(an[0].pos!=0)printf("The maximum travel distance = 0.00");else{double res=0,dis=0,remain=0;int gs=0;priority_queue<struct petrol> q;maxd=c*p;q.push(petrol(c,an[gs].price));remain=c;while(fabs(d-dis)>=eps){int flag=1,mini=gs;double min=10000000,sub,cost;for(int i=gs+1;an[i].pos-an[gs].pos<=maxd&&i<=n;i++){if(an[gs].price>an[i].price){sub=an[i].pos-an[gs].pos; cost=sub/p;while(!q.empty()&&cost!=0){if(q.top().vol>cost){res+=cost*q.top().price;q.pop();cost=0;}else{remain-=q.top().vol;cost-=q.top().vol;res+=q.top().vol*q.top().price;q.pop();}}while(!q.empty()&&cost!=0)q.pop();q.push(petrol(c,an[i].price));dis+=sub;remain=c;gs=i;flag=0;break;}else if(an[gs].price<=an[i].price&&an[i].price<min){min=an[i].price;mini=i;}}if(mini==gs){while(!q.empty()){dis+=q.top().vol*p;q.pop();}break;}if(flag){sub=an[mini].pos-an[gs].pos;cost=sub/p;while(!q.empty()&&cost!=0){if(q.top().vol>cost){struct petrol Q=q.top();remain-=cost;Q.vol-=cost;res+=cost*q.top().price;q.pop();q.push(Q); cost=0;}else{remain-=q.top().vol;cost-=q.top().vol;res+=q.top().vol*q.top().price;q.pop();}}q.push(petrol(c-remain,an[mini].price));remain=c;dis+=sub;gs=mini;}		}if(dis==d)printf("%.2f",res);elseprintf("The maximum travel distance = %.2f",dis);}return 0;
}





这篇关于pat-advanced-1033的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/602613

相关文章

PAT甲级-1044 Shopping in Mars

题目   题目大意 一串项链上有n个钻石,输入给出每个钻石的价格。用m元买一个连续的项链子串(子串长度可为1),如果不能恰好花掉m元,就要找到最小的大于m的子串,如果有重复就输出多个,按递增顺序输出子串的前端和后端索引。 原来的思路 取连续的子串使和恰等于m,没有恰等于就找最小的大于。可以将子串依次累加,使得每个位置都是起始位置到该位置的序列和,整个数组显递增顺序,就可以用右边减左边

PAT (Advanced Level) Practice——1011,1012

1011:  链接: 1011 World Cup Betting - PAT (Advanced Level) Practice (pintia.cn) 题意及解题思路: 简单来说就是给你3行数字,每一行都是按照W,T,L的顺序给出相应的赔率。我们需要找到每一行的W,T,L当中最大的一个数,累乘的结果再乘以0.65,按照例子写出表达式即可。 同时还需要记录每一次选择的是W,T还是L

【持续更新】Advanced Download Manager 14.0.35 Pro安卓ADM下载神器最新高级免费修改版

这个也算小有名气,名字和 idm 有点像。当程序从剪贴板中截取链接后,您可以将其复制并发送至ADM编辑器,或者使用“添加”按钮粘贴链接。 ▨ ADM 有以下特点: • 该应用支持同时下载最多三个文件 • 通过多线程技术(9个部分)加速下载过程 • 从安卓浏览器及剪贴板中拦截链接 • 后台下载文件,并在失败后自动恢复 • 支持图片、文档、压缩包及程序的加载 • 针对Lollipop和Ma

【Agent】Agent Q: Advanced Reasoning and Learning for Autonomous AI Agents

1、问题背景 传统的训练Agent方法是在静态数据集上进行监督预训练,这种方式对于要求Agent能够自主的在动态环境中可进行复杂决策的能力存在不足。例如,要求Agent在web导航等动态设置中执行复杂决策。 现有的方式是用高质量数据进行微调来增强Agent在动态环境中的决策能力,但这往往会出现复合错误和有限的探测数据,最终导致结果不够理想。 2、提出方法 Agent Q 框架将蒙特卡洛树搜

Zabbix 企业级高级应用(Zabbix Enterprise Advanced Application)

💝💝💝欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。 推荐:Linux运维老纪的首页,持续学习,不断总结,共同进步,活到老学到老 导航剑指大厂系列:全面总结 运维核心技术:系统基础、数据库、网路技术、系统安全、自动化运维、容器技术、监控工具、脚本编程、云服务等。 常用运维工具系列:常

PAT (Advanced Level) Practice

1001:  题目大意: 计算 a+b 的结果,并以标准格式输出——即每三个数字一组,组之间用逗号分隔(如果数字少于四位,则不需要逗号分隔)  解析: 我们知道相加右正有负,对于样例来说 Sample Input: -1000000 9 Sample Output: -999,991 如果是从左往右,算上负号的话输出应该是-99,999,1 从右往左:-,999,991离正确

基于医学图像配准软件 ANTs(Advanced Normalization Tools)提取脑图像数值并与临床量表计算相关

前言: 神经影像学与临床评估的结合正在革新我们对神经精神疾病的理解。本博客聚焦于如何利用先进的医学图像配准软件ANTs(Advanced Normalization Tools)提取脑图像数值,并将其与临床量表进行相关性分析。 目录   一、准备掩模(Mask) 二、准备T-value map T-map 和 Z-map的转化 比较同一结果的T-map和Zmap 三、提取Mask

【Material-UI】Select 组件的Advanced features和Props

文章目录 一、Select 组件概述1. 组件介绍2. 高级功能概述 二、Select 组件的基础用法三、Select 组件的高级功能1. 多选(Multiselect)2. 自动完成(Autocomplete)3. 异步加载(Async)4. 可创建选项(Creatable) 四、Select 组件的属性详解1. variant 属性2. Props 属性3. 标签与辅助文本 五、总结

The International Journal of Advanced Manufacturing Technology投稿相关记录

特刊链接 https://link.springer.com/collections/ffdjajgahc 下载Latex模板 https://www.iotword.com/17785.html 改为双栏 分区 图片转换为eps格式 https://convertio.co/zh/pdf-eps/ 编译超时 注释75,82,86可以了 编译再次超时 改用TeXworks

1050 String Subtraction——PAT甲级

Given two strings S1​ and S2​, S=S1​−S2​ is defined to be the remaining string after taking all the characters in S2​ from S1​. Your task is simply to calculate S1​−S2​ for any given strings. However,