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. SurfaceFlinger 讲解

    SurfaceFlinger是Android multimedia的一个部分,在Android 的实现中它是一个service,提供系统 范围内的surface composer功能,它能够将各种应用 ...

  2. vsftpd.conf 详解

    //不允许匿名访问 anonymous_enable=NO //设定本地用户可以访问.注意:主要是为虚拟宿主用户,如果该项目设定为NO那么所有虚拟用户将无法访问 local_enable=YES // ...

  3. docker swarm join 报错

    [peter@minion ~]$ docker swarm join --token SWMTKN-1-3mj5po3c7o04le7quhkdhz6pm9b8ziv3qe0u7hx0hrgxsna ...

  4. java基础4 函数

    本文知识点(目录): 1.函数的概述    2.函数的格式    3.自定义函数    4.函数的特点    5.函数的应用    6.函数的重载 1.函数的概述 发现不断进行加法运算,为了提高代码的 ...

  5. 端口扫描———nmap

    nmap教程之nmap命令使用示例(nmap使用方法) 浏览:8268 | 更新:2014-03-29 17:23 Nmap是一款网络扫描和主机检测的非常有用的工具.Nmap是不局限于仅仅收集信息和枚 ...

  6. python_线程、进程和协程

    线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python #coding=utf-8 __author__ = 'yinjia' i ...

  7. 以太坊go-ethereum客户端docker安装(一)

    最近一段时间忙于工作,就没来得及发表博客,但一直没有停止对区块链的研究.周末抽时间分享一下近期比较重大的收获之一--使用docker来搭建和使用以太坊的节点.本人已经顺利搭建出,开发环境,测试环境,F ...

  8. beego学习笔记(4):开发文档阅读(1)

    1.beego的设计是高度模块化的.每个模块,都可以单独使用.一共八大模块: cache;session;log;orm;context;httplibs;toolbox 2.beego的执行逻辑 3 ...

  9. 洛谷 P2708 硬币翻转 题解

    题目传送门 真如题面所说,难度系数:☆☆☆☆☆(如果你看懂了). 从后往前扫一次,如果a[i]==0&&a[i-1]==1那么将ans+2. 注意最后不要忘记开头if(a[0]=='0 ...

  10. DotNetOpenAuth实践之WebApi资源服务器

    系列目录: DotNetOpenAuth实践系列(源码在这里) 上篇我们讲到WCF服务作为资源服务器接口提供数据服务,那么这篇我们介绍WebApi作为资源服务器,下面开始: 一.环境搭建 1.新建We ...