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. 海洋CMS v6.53 v6.54命令执行

    测试下载地址:https://pan.baidu.com/s/1jHQBKFk 至于分析实在是看的一脸懵逼就不累赘了.直接上exp POST /haiyang/upload/search.php HT ...

  2. route add提示: "SIOCADDRT: No such process

    解决方法如下: 原因: There are multiple known causes for this error: - You attempted to set a route specific ...

  3. WebBrowser中运行js

    HtmlElement script = wf.WebBrowser.Document.CreateElement("script"); script.SetAttribute(& ...

  4. git 还原到指定版本号

      git clone git branch -r --contains 88b92060224e96ef209565fa75c816eb9b0fae8e git checkout origin/re ...

  5. Linux shell中运行命令后加上字符“&”的作用(转)

    原文链接为:http://blog.sina.com.cn/s/blog_963453200102uya7.html & 放在启动参数后面表示设置此进程为后台进程 默认情况下,进程是前台进程, ...

  6. 初探Nginx架构

    参考链接:http://tengine.taobao.org/book/chapter_02.html nginx在启动后,在unix系统中会以daemon的方式在后台运行,后台进程包含一个maste ...

  7. mysql 配置数据库主从同步

    参考:https://www.cnblogs.com/kevingrace/p/6256603.html http://www.51testing.com/html/00/130600-243651. ...

  8. [ python ] 字典的使用

    数据类型划分:    可变数据类型:list.dict.set    不可哈希    不可变数据类型:tuple.bool.int.str    可哈希 字典 python内置了字典类型,使用键-值( ...

  9. gbdt和xgboost中feature importance的获取

    来源于stack overflow,其实就是计算每个特征对于降低特征不纯度的贡献了多少,降低越多的,说明feature越重要 I'll use the sklearn code, as it is g ...

  10. css绘制图标

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...