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

    更改 Vuex 的 store 中的状态的唯一方法是提交 mutation.Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 ...

  2. Ubuntu 17.10 用 apt 搭建 lamp 环境(精简版)

    这篇文章主要用来快速部署以 php 5.6 为主的 lamp 环境,要看详细安装包括虚拟主机配置的请参考这篇:http://www.cnblogs.com/mingc/p/7864030.html 一 ...

  3. 21.Merge Two Sorted Lists---《剑指offer》面试17

    题目链接:https://leetcode.com/problems/merge-two-sorted-lists/description/ 题目大意: 给出两个升序链表,将它们归并成一个链表,若有重 ...

  4. [转载]理解Tomcat的Classpath-常见问题以及如何解决

    摘自: http://www.linuxidc.com/Linux/2011-08/41684.htm 在很多Apache Tomcat用户论坛,一个问题经常被提出,那就是如何配置Tomcat的cla ...

  5. 转:google测试分享-问题和挑战

    原文: http://blog.sina.com.cn/s/blog_6cf812be0102vxer.html 前言:这个系列分享的内容大部分都是出自于<google是如何测试的>的书, ...

  6. C#窗口矩形区域着色

    C#写的一个GUI窗口,有几百个矩形区域.每个矩形区域的颜色随时都可能改变,并且多次改变. 我放弃使用label绘制矩形,因为效果不好.拖控件的界面使用power packs中的rectanglesh ...

  7. Linux下Diff命令

    一般正常比较两个文件用vimdiff,算是直接进入vim界面,如果比较两个文件夹下面的文件,可以用diff,注意,这里只会比较文件夹下面的同名文件,他会列出不一样的点. 参考Linux下Diff命令使 ...

  8. vuejs学习——vue+vuex+vue-router项目搭建(一)

    前言 快年底了却有新公司邀请了我,所以打算把上家公司的学到一下技术做一些总结和分享. 现在vuejs都2.0了,我相信也有很多朋友和我一样实际项目还是选择vue1.0的或者给新手一些参考,不管在选择哪 ...

  9. Redis实战(六)

    查询数据 1.使用Linq匹配关键字查询 using (var redisClient = RedisManager.GetClient()) { var user = redisClient.Get ...

  10. logstash通过redis收集日志

    (1)部署redis 1丶安装redis yum install epel-release -y yum install redis -y 2丶修改配置文件 #vim /etc/redis.conf ...