PAT甲级1033. To Fill or Not to Fill

题意:

有了高速公路,从杭州到任何其他城市开车很容易。但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站。不同的加油站可能会给不同的价格。您被要求仔细设计最便宜的路线。

输入规格:

每个输入文件包含一个测试用例。对于每种情况,第一行包含4个正数:Cmax(<= 100),坦克的最大容量; D(<= 30000),杭州与目的地城市的距离; Davg(<= 20),汽车可以运行的单位气体的平均距离;和N(<= 500),加油站总数。

随后N行,每个包含一对非负数:Pi,单位气体价格,Di(<= D),本站与杭州之间的距离,i = 1,... N。一行中的所有数字都以空格分隔。

输出规格:

对于每个测试用例,打印一行中最便宜的价格,精确到2位小数。

假设坦克在开始时是空的。如果不可能到达目的地,请打印“最大行驶距离= X”,其中X是汽车可以运行的最大可能距离,精确到2位小数。

思路:

贪心。

  • 结果保留两位小数
  • 第一个加油站必须在0
//最后一站。判断是否能到达destination。

//非最后一战。向后搜索maxlen内第一个便宜或者等价的station
//有的话,停止搜索,买刚好用完的汽油。并且以这个station为下一个站台continue
//没有的话,判断能否到达destination。
//能到达destination,买刚好用完的汽油去destination,break
//不能的话,买满汽油,去价格最便宜的一个station作为下一个站台。

ac代码:

C++

// pat1033.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<unordered_map> using namespace std;
//cheapest prices or max_distance 小数点后两位
//station[0].dis 必须等于 0 ; struct sta
{
double dis;
double price;
}; int capacity, destination, d_avg, nums; //100, 3e5, 20 , 500
sta station[505]; bool stacmp(sta& a, sta& b)
{
return a.dis < b.dis;
} int main()
{
cin >> capacity >> destination >> d_avg >> nums;
int maxlen = d_avg * capacity; for(int i = 0; i < nums; i++)
{
cin >> station[i].price >> station[i].dis;
}
sort(station, station + nums,stacmp); int cur = 0;
double res = 0,extra = 0,maxdis = 0;
while (station[0].dis == 0 && cur < nums)
{
//cout << "当前station: " << cur << endl; //最后一站。判断是否能到达destination。 //非最后一战。向后搜索maxlen内第一个便宜或者等价的station
//有的话,停止搜索,买刚好用完的汽油。并且以这个station为下一个站台continue
//没有的话,判断能否到达destination。
//能到达destination,买刚好用完的汽油去destination,break
//不能的话,买满汽油,去价格最便宜的一个station作为下一个站台。 if (cur == nums - 1)
{
if (station[cur].dis + maxlen >= destination)
{
res += ((destination - station[cur].dis) / d_avg - extra) * station[cur].price;
extra = 0;
break;
}
else
{
res = 0;
maxdis = station[cur].dis + maxlen;
break;
}
} int next = cur + 1, minindex = cur + 1;
double minst = station[cur + 1].price;
bool flag = false;
while (next < nums && station[cur].dis + maxlen >= station[next].dis)
{
if (station[next].price <= station[cur].price)
{
res += ((station[next].dis - station[cur].dis) / d_avg - extra) * station[cur].price;
//cout << res << endl;
extra = 0;
flag = true;
cur = next;
break;
}
if (station[next].price < minst)
{
minst = station[next].price;
minindex = next;
}
next++;
} if (flag) continue;
if (station[cur].dis + maxlen >= destination)
{
res += ((destination - station[cur].dis) / d_avg - extra) * station[cur].price;
//cout << res << endl;
extra = 0;
break;
}
else
{
res += (capacity - extra) * station[cur].price;
extra = capacity - (station[minindex].dis - station[cur].dis) / d_avg;
//cout << res << endl;
cur = minindex;
}
} if (res == 0) printf("The maximum travel distance = %.2f\n", maxdis);
else printf("%.2f\n", res);
return 0;
}

PAT甲级1033. To Fill or Not to Fill的更多相关文章

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

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

  3. 【贪心】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 ...

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

  5. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  6. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  7. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

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

  9. PAT甲级目录

    树(23) 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree 判断BST,BST的性质 ...

随机推荐

  1. flask基础之jijia2模板使用基础(二)

    前言 在以前前后端不分离的时代,后台程序员往往又当爹又当妈,需要将前端程序员写的h5页面填充模板语言.而jijia2是一门十分强大的python的模板语言,是flask框架的核心模块之一.先简单介绍一 ...

  2. Python os模块和sys模块 操作系统的各种接口

    一.os模块 这个模块提供了一个便携式去使用操作系统的相关功能,如果只是想操作路径,请参阅os.path模块. ''' os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 ...

  3. Java Web 远程调试

    Java Web 远程 调试 Tomcat 下载压缩版服务器 环境:Tomcat.Eclipse,做远程调试我们并不需要其他特殊插件 1.配置Tomcat/bin/startup.bat 在前面增加代 ...

  4. LAMP网站架构解释

    对于大流量.大并发量的网站系统架构来说,除了硬件上使用高 性能的服务器.负载均衡.CDN等之外,在软件架构上需要重点关注下面几个环节:使用高性能的操作系统(OS).高性能的网页服务器(Web Serv ...

  5. LINUX下解决netstat查看TIME_WAIT状态过多问题(转)

    原文连接:www.itokit.com/2012/0516/73950.html # netstat -an|awk '/tcp/ {print $6}'|sort|uniq -c 16 CLOSIN ...

  6. PYTHON学习(三)之利用python进行数据分析(1)---准备工作

    学习一门语言就是不断实践,python是目前用于数据分析最流行的语言,我最近买了本书<利用python进行数据分析>(Wes McKinney著),还去图书馆借了本<Python数据 ...

  7. 20165301 2017-2018-2 《Java程序设计》第四周学习总结

    20165301 2017-2018-2 <Java程序设计>第四周学习总结 教材学习内容总结 第五章:子类与继承 一个类只能有一个父类,但是可以有若干个子类. 子类的继承性 子类和父类在 ...

  8. hdu 5468(dfs序+容斥原理)

    Puzzled Elena Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. csu 1547(01背包)

    1547: Rectangle Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 996  Solved: 277[Submit][Status][Web ...

  10. (转)HttpClient 模拟登陆,保持会话并进行后续操作

    转自:http://unmi.cc/httpclient-login-session android实现session保持 SessionID的本质