PAT甲级1033. To Fill or Not to Fill
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的更多相关文章
- 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 ...
- 【贪心】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甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
- 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甲级目录
树(23) 备注 1004 Counting Leaves 1020 Tree Traversals 1043 Is It a Binary Search Tree 判断BST,BST的性质 ...
随机推荐
- 一个python爬虫工具类
写了一个爬虫工具类. # -*- coding: utf-8 -*- # @Time : 2018/8/7 16:29 # @Author : cxa # @File : utils.py # @So ...
- HOJ 1108
题目链接:HOJ-1108 题意为给定N和M,找出最小的K,使得K个N组成的数能被M整除.比如对于n=2,m=11,则k=2. 思路是抽屉原理,K个N组成的数modM的值最多只有M个. 具体看代码: ...
- python ORM - sqlalchemy 操作使用
python操作数据库 使用 ORM - sqlalchemy,pymsql 安装: pip install pymsq pip install sqlalchemy 一. '''连接数据库''' ...
- redis常见数据操作
redis中有5种常见的数据类型,针对这5种数据类型有着相应的数据操作. 1.String(键值对为String - String) set k1 v1 get k1 getset k1 v1 - h ...
- 一个带重试次数的curl 函数
<?php/** * [curl 带重试次数] * @param [type] $url [访问的url] * @param [type] $post [$POST参数] * @param in ...
- python开发学习-day08(socket高级、socketserver、进程、线程)
s12-20160305-day08 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- Java虚拟机四:垃圾回收算法与垃圾收集器
在Java运行时的几个数据区域中,程序计数器,虚拟机栈,本地方法栈3个区域随着线程而生,随线程而灭,因此这几个区域的内存分配和回收具有确定性,不需要过多考虑垃圾回收问题,因为方法结束或者线程结束时,内 ...
- 【LOJ】#2085. 「NOI2016」循环之美
题解 我们要求的其实是这个东西= = \(\sum_{i = 1}^{n}\sum_{j = 1}^{n}[(i,j) == 1][(j,k) == 1]\) 然后变一下形 \(\sum_{j = 1 ...
- 一个配置文件收集多个日志-if根据type类型判断
1.同时收集/var/log/messages日志和secure日志 #vim /etc/logstash/conf.d/system.conf input { file { path => & ...
- mp4文件数据格式解析
unsigned int(32)[3] 32*3bit string[32] 32*8bit class VisualSampleEntry(codingname) extends Sampl ...