题目描写叙述

With highways available, driving a car from Hangzhou to any other city is easy.  But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time.  Different gas station may give different price.  You are asked to carefully design the cheapest route to go.

输入描写叙述:

Each input file contains one test case.  For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations.  Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N.  All the numbers in a line are separated by a space.

输出描写叙述:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places.  It is assumed that the tank is empty at the beginning.  If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

输入样例:

50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300

输出样例:

749.17
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <climits> using namespace std; typedef struct Node
{
double price;
double distance;
}Node; bool cmp(const Node& lhs, const Node& rhs)
{
return lhs.distance<rhs.distance;
} int main()
{
double Cm,D,cur,cost,minCost,maxDistance;
int Da,N;
int i,j,k,l,ans;
while(cin>>Cm>>D>>Da>>N)
{
maxDistance=Cm*Da;
Node *node=new Node[N+1];
for(i=0;i<N;i++)
cin>>node[i].price>>node[i].distance; sort(node,node+N,cmp); node[N].price=0;
node[N].distance=D; ans=cur=cost=0;
for(i=0;i<N-1;i++)
{
if((node[i+1].distance-node[i].distance)>maxDistance)
break;
}
if(i<N-1)
{
printf("The maximum travel distance = %.2lf\n",node[i].distance+maxDistance);
}
else
{
for(i=0;i<N;)
{ //后面假设有比当前更廉价的汽油,如今先少弄点,够到那个地方就OK
l=i;
minCost=node[i].price;
for(j=i+1;j<=N && (node[i].distance+maxDistance>=node[j].distance);j++)
{
if(minCost>node[j].price)
{
l=j;
break;
}
}
if(l!=i)
{
cost=cost+((node[l].distance-node[i].distance)/Da-cur)*node[i].price;
cur=0;
i=l;
continue;
}
//假设找不到更廉价的加油站。找出相对最廉价的加油站。而且在当前加油站加满油
l=i;
minCost=1e18;
for(j=i+1;j<=N && (node[i].distance+maxDistance>=node[j].distance);j++)
{
if(minCost>node[j].price)
{
minCost=node[j].price;
l=j;
}
}
cost=cost+(Cm-cur)*node[i].price;
cur=Cm-(node[l].distance-node[i].distance)/Da;
i=l;
}
printf("%.2lf",cost);
}
/* for(i=0;i<N;i++)
cout<<node[i].price<<" "<<node[i].distance<<endl; */
}
return 0;
}

贴个图

PAT 1004 To Fill or Not to Fill (25)的更多相关文章

  1. PAT甲级1033. To Fill or Not to Fill

    PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...

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

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

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

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

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

  7. PAT_A1033#To Fill or Not to Fill

    Source: PAT A1033 To Fill or Not to Fill (25 分) Description: With highways available, driving a car ...

  8. 1033. To Fill or Not to Fill (25)

     题目链接:http://www.patest.cn/contests/pat-a-practise/1033 题目: 1033. To Fill or Not to Fill (25) 时间限制 1 ...

  9. 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 other ...

  10. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

随机推荐

  1. asp.net 自定义的模板方法接口通用类型

    本来想写这个帖子已经很久了,但是公司事情多,做着做着就忘记了.公司因为需要做接口,而且用的还是asp.net的老框架,使用Handler来做,没得办法,自己照着MVC写了一个通过的接口操作模板. 上送 ...

  2. SharePoint Online 创建用户和组

    前言 本文介绍如何在Office 365中创建用户和组,这里所说的用户和组,是指Office 365中的用户和组,我们可以用这里的用户登录Office 365环境,用组的概念来管理用户,而非Share ...

  3. robotframework自动化系列:新增流程

    刚接手项目的时候,要求所有流程在上线之前必须确保正向操作是正确的:这个时候又有新的模块需要测试,所以引入自动化测试是非常有必要的!通过对比,尝试使用RF进行自动化的回归测试. 测试中最常见的操作就是增 ...

  4. mybatis从数据库中取到的date格式不是yyyy-MM-dd HH:mm:ss

    问题:sqlserver中的存储时间格式为date,pojo的时间属性也是date,直接mybatis取出的时间格式是带英语的那种,不满足客户要求. 解决:将pojo的时间属性改为string类型,在 ...

  5. ASP.NET没有魔法——ASP.NET MVC Controller的实例化与执行

    上一章节中对路由的注册和匹配过程进行了介绍,知道了MVC的Http请求最终是交由MvcHandler处理的,而其处理过程就是对Controller的创建.执行和释放. 本章将从以下几点进一步对上面提到 ...

  6. centos安装maven,git,mysql

    安装maven1:下载maven包:http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-bi ...

  7. Python——正则表达式特殊符号及用法

    由于正则表达式的内容比较多,所以单独写成一系列文章,主要内容是根据小甲鱼所讲的内容综合一下正则表达式的笔记. 贴上小甲鱼的<Python3 如何优雅地使用正则表达式>系列可观看的博客地址: ...

  8. set 利用lower_bound实现key索引

    set中数据类型为结构体T,T中有两个成员key和val定义如下: struct T{ int key,val; T(int k,int v):key(k),val(v){} bool operato ...

  9. 通过hadoop + hive搭建离线式的分析系统之快速搭建一览

    最近有个需求,需要整合所有店铺的数据做一个离线式分析系统,曾经都是按照店铺分库分表来给各自商家通过highchart多维度展示自家的店铺经营 数据,我们知道这是一个以店铺为维度的切分数据,非常适合目前 ...

  10. SSM框架开发web项目系列(一) 环境搭建篇

    前言 开发环境:Eclipse Mars + Maven + JDK 1.7 + Tomcat 7 + MySQL 主要框架:Spring + Spring MVC + Mybatis 目的:快速上手 ...