PAT 1004 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 different price. You are asked to carefully design the cheapest route to go.
输入描写叙述:
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.
输出描写叙述:
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.
输入样例:
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
输出样例:
749.17
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <climits> using namespace std; typedef struct Node
{
double price;
double distance;
}Node; bool cmp(const Node& lhs, const Node& rhs)
{
return lhs.distance<rhs.distance;
} int main()
{
double Cm,D,cur,cost,minCost,maxDistance;
int Da,N;
int i,j,k,l,ans;
while(cin>>Cm>>D>>Da>>N)
{
maxDistance=Cm*Da;
Node *node=new Node[N+1];
for(i=0;i<N;i++)
cin>>node[i].price>>node[i].distance; sort(node,node+N,cmp); node[N].price=0;
node[N].distance=D; ans=cur=cost=0;
for(i=0;i<N-1;i++)
{
if((node[i+1].distance-node[i].distance)>maxDistance)
break;
}
if(i<N-1)
{
printf("The maximum travel distance = %.2lf\n",node[i].distance+maxDistance);
}
else
{
for(i=0;i<N;)
{ //后面假设有比当前更廉价的汽油,如今先少弄点,够到那个地方就OK
l=i;
minCost=node[i].price;
for(j=i+1;j<=N && (node[i].distance+maxDistance>=node[j].distance);j++)
{
if(minCost>node[j].price)
{
l=j;
break;
}
}
if(l!=i)
{
cost=cost+((node[l].distance-node[i].distance)/Da-cur)*node[i].price;
cur=0;
i=l;
continue;
}
//假设找不到更廉价的加油站。找出相对最廉价的加油站。而且在当前加油站加满油
l=i;
minCost=1e18;
for(j=i+1;j<=N && (node[i].distance+maxDistance>=node[j].distance);j++)
{
if(minCost>node[j].price)
{
minCost=node[j].price;
l=j;
}
}
cost=cost+(Cm-cur)*node[i].price;
cur=Cm-(node[l].distance-node[i].distance)/Da;
i=l;
}
printf("%.2lf",cost);
}
/* for(i=0;i<N;i++)
cout<<node[i].price<<" "<<node[i].distance<<endl; */
}
return 0;
}
贴个图
PAT 1004 To Fill or Not to Fill (25)的更多相关文章
- PAT甲级1033. To Fill or Not to Fill
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- 【贪心】PAT 1033. To Fill or Not to Fill (25)
1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...
- PAT 1033 To Fill or Not to Fill[dp]
1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other ...
- 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 oth ...
- 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 i ...
- 1033 To Fill or Not to Fill
PAT A 1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other ...
- PAT_A1033#To Fill or Not to Fill
Source: PAT A1033 To Fill or Not to Fill (25 分) Description: With highways available, driving a car ...
- 1033. To Fill or Not to Fill (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1033 题目: 1033. To Fill or Not to Fill (25) 时间限制 1 ...
- 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 ...
- 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题
题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...
随机推荐
- RPA(Robotic Process Automation)的概要介绍
最近因为公司业务的需要,开始关注RPA的内容,奈何国内相关的信息太少,只能硬着头皮啃英文了. 下面记录的内容作为学习笔记,有不对的地方请大家指教. 首先RPA(Robotic Process Auto ...
- 【吐槽】关于256个 class可以覆盖一个id的问题
还是说今天下午面试的事情,被面试官问了 40多分钟的问题,我觉得丫 一定是从哪个网站down了几份面试题,自个儿整合了一下,然后挨个问,刚开始感觉哟,不错哦,面试官懂的蛮多的. 然后问到某个问题之后, ...
- ldap数据库--ODSEE--schema
ldap服务器包含上百个对象类型(object class)和属性,这些对象类和属性都可以满足大部分需求,如果你想定义自己的schema,你只能继承扩展现有的schema进行操作. tip: 增加的新 ...
- 开源API测试工具 Hitchhiker v0.4更新 - 没有做不到,只有想不到
Hitchhiker 是一款开源的 Restful Api 测试工具,支持Schedule, 数据对比,压力测试,支持上传脚本定制请求,可以轻松部署到本地,和你的team成员一起管理Api. 详细介绍 ...
- [转载] Netty教程
转载自http://blog.csdn.net/kobejayandy/article/details/11493717 先啰嗦两句,如果你还不知道Netty是做什么的能做什么.那可以先简单的搜索了解 ...
- 用JAVA中BufferedImage画出漂亮的验证码点击变化
如果我们想用JAVA中BufferedImage画出漂亮的验证码点击变化怎么实现呢,类似这样: 点击变化,以下是实现过程,直接上代码: 首先前台:<i><img style=&quo ...
- JavaWeb面试(六)
51.说一说Servlet的生命周期? Servlet有良好的生存期的定义,包括加载和实例化.初始化.处理请求以及服务结束.这个生存期由javax.servlet.Servlet接口的init(),s ...
- Sublime Text 3 修改配色方案
你可能会觉得 Sublime Text 配色方案的颜色(注释.背景色)看起来不习惯,其他都满意.此时我们可以自己修改这些配色,不需要更换整个配色方案. 需要安装 PackageResourceView ...
- java中a=a+1和a+=1的区别
我们先看一段代码: 1 byte b=2; b=b+1; System.out.println(b); 运行结果: 错误: 不兼容的类型: 从int转换到byte可能会有损失 ...
- JSP4(内置对象)
一.超级链接传值问题传值: <a href="href1.jsp?id=1&name=pkd&age=40">This is my JSP page. ...