Road Trip
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 29, Accepted users: 29
Problem 12882 : No special judgement
Problem description

You are planning a road trip to visit your friends, each of whom live in different towns. Of course, you don't want to pay any more for fuel on the trip than you need to. However, the price of fuel in each of the towns is different, so you will need to carefully plan the trip to minimise the cost. For example, it might make sense to take on extra fuel at a town where the price is low so that you don't need to buy so much at a town where it is more expensive. Indeed, it may even make sense to sell excess fuel at some towns to recoup some of the costs. Of course, your car can only hold a certain amount of fuel, and you have to make sure you take on enough fuel at each town to reach the next (assume that it's OK to arrive with an empty tank).
Your task is to write a program to
help you plan your trip.

Input

Input will consist of specifications for a series of journeys. Information
for each journey will begin with a line containing an integer 0 < c < 100
that specifies the capacity of the car's fuel tank in litres and an integer 0
< t < 20 that specifies the number of towns to visit for that journey. A
line containing two zeros indicates the end of input.
The following t lines
contain information about successive stages on the journey: the price (in fixed
point dollars-and-cents format, 0.01 <= p < 9.99) of one litre of fuel
(either to buy or to sell) in the town at the beginning of the stage, and the
number of litres of fuel (an integer, 1 <= n < 100) needed to reach the
next town.

Output

Output should consist of one line for each journey comprising the journey
number (formatted as shown) followed by a single space and the minimum cost of
completing that journey, formatted as a fixed-point number with 2 decimal
places.

Sample Input
10 3
2.00 7
1.50 8
1.00 3
50 6
1.50 20
4.20 5
1.15 35
1.41 27
1.92 30
2.21 15
0 0
Sample Output
Journey 1: 29.00
Journey 2: 117.64
Problem Source
HNU Contest 

Mean:

你要开车去n个小镇拜访你的老朋友,但是你不是高富帅,只得将油钱尽量的省,现在给你油箱的容量、小镇数量、每个小镇的油价、到达下一个小镇需要的油,你可以买油也可卖油,问你最少的油钱是多少。

analyse:

这题就是一个贪心的思想,如果下一站的油价比这站高,那么别想了,将油箱装满,到下一站就可以赚钱;反之就只需买够路上用的就可。

当然有人说这题是dp,这也没错,看个人怎么理解,其实这题就两个状态之间转移,dp已经退化为贪心。

Time complexity:O(n)

Source code:

//Memory   Time
// 1254K    0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAX 110
#define LL long long
using namespace std;
struct Node
{
   double p;
   int next;
};
Node node[MAX];
int v,n;
int main()
{
   int kase=;
   while(scanf("%d %d",&v,&n),v+n)
   {
       double cost=0.0;
       printf("Journey %d: ",kase++);
       bool flag=;
       for(int i=;i<=n;i++)
       {
           scanf("%lf %d",&node[i].p,&node[i].next);
           if(node[i].next>v)
           {
               flag=;
               break;
           }
       }
       if(flag==)
       {
           puts("0.00");
           continue;
       }
       int now=;
       for(int i=;i<n;i++)   //zui hou mei pan
       {

if(i>) // begin to No.2
           {
               if(node[i].p>node[i-].p&&now>node[i].next)   //sell
               {
                   int tmp=now-node[i].next;
                   cost-=node[i].p*tmp;
                   now-=tmp;
               }
           }

if(node[i].p<node[i+].p)   //  earn money
           {
               int tmp=v-now;
               cost+=node[i].p*tmp;
               now=v-node[i].next;
           }
           else    // bu ke zhuan qian
           {
               int tmp;
               if(node[i].next>now)
               {
                   tmp=node[i].next-now;
                   now=;
               }
               else
               {
                   tmp=;
                   now-=node[i].next;
               }
               cost+=node[i].p*tmp;
           }

}
       // final
       if(now>node[n].next)
       {
           int tmp=now-node[n].next;
           cost-=tmp*node[n].p;
           now=;
       }
       else
       {
           int tmp=node[n].next-now;
           cost+=tmp*node[n].p;
           now=;
       }

printf("%.2lf\n",cost);
   }
   return ;
}

dp or 贪心 --- hdu : Road Trip的更多相关文章

  1. P1095 守望者的逃离——DP?贪心?

    https://www.luogu.org/problem/P1095 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大 ...

  2. P2279 消防局的设立 (树形DP or 贪心)

    (点击此处查看原题) 树形DP写法 看到这个题的要求,很容易相到这是一个树形DP的问题,但是dp数组应该如何设计并转移才是关键 dp[i][0]代表当前结点可以向上覆盖2层,自身一定被覆盖dp[i][ ...

  3. hdu 1257 && hdu 1789(简单DP或贪心)

    第一题;http://acm.hdu.edu.cn/showproblem.php?pid=1257 贪心与dp傻傻分不清楚,把每一个系统的最小值存起来比较 #include<cstdio> ...

  4. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  5. URAL 1203 Scientific Conference dp?贪心

    题目:click here 分明就是贪心怎么会在dp的专题 #include <bits/stdc++.h> using namespace std; typedef unsigned l ...

  6. luoguP1281 书的复制 DP,贪心

    luoguP1281 书的复制 链接 https://www.luogu.org/problemnew/show/P1281 思路 简单dp,输出方案. 很明显dp记录路径对不对? 恭喜你死了. 求出 ...

  7. CF101D Castle 树形DP、贪心

    题目传送门 题意:给出一个有$N$个点的树,你最开始在$1$号点,经过第$i$条边需要花费$w_i$的时间.每条边只能被经过$2$次.求出到达除$1$号点外所有点的最早时间的最小平均值.$N \leq ...

  8. [BZOJ4391][Usaco2015 dec]High Card Low Card dp+set+贪心

    Description Bessie the cow is a huge fan of card games, which is quite surprising, given her lack of ...

  9. 2018.08.29 NOIP模拟 movie(状压dp/随机化贪心)

    [描述] 小石头喜欢看电影,选择有 N 部电影可供选择,每一部电影会在一天的不同时段播 放.他希望连续看 L 分钟的电影.因为电影院是他家开的,所以他可以在一部电影播放过程中任何时间进入或退出,当然他 ...

随机推荐

  1. 算法:x的n次方

    该题是用来公司教学,并无难度.用于说明算法效率差异以及循环和递归的效率差别. package practice; import java.math.BigDecimal; /** * @author ...

  2. Javascript事件模型系列(四)我所理解的javascript自定义事件

    被我拖延了将近一个月的javascript事件模型系列终于迎来了第四篇,也是我计划中的最后一篇,说来太惭愧了,本来计划一到两个星期写完的,谁知中间遇到了很多事情,公司的个人的,搞的自己心烦意乱浮躁了一 ...

  3. 招聘:有兴趣做一个与Android对等的操作系统么?

    招聘:有兴趣做一个与Android对等的操作系统么? 前不久我发了一篇<八一八招聘的那些事儿>讲了我自己作为求职者对招聘的一些看法.那个时候我还在求职,对求职的结果还是挺满意的,五家公司面 ...

  4. 赴美工作常识(Part 3 - 英语)

    在<Part 2 - 申请>的评论中有人问英语要达到何种水平,以及如何提高.其实英语也不是我的强项,只是刚刚好做到能够沟通而已.由于我在知乎上回到过一个类似问题,我就基于那个答案简单说一下 ...

  5. 细说.NET中的多线程 (五 使用信号量进行同步)

    上一节主要介绍了使用锁进行同步,本节主要介绍使用信号量进行同步 使用EventWaitHandle信号量进行同步 EventWaitHandle主要用于实现信号灯机制.信号灯主要用于通知等待的线程.主 ...

  6. ECMAScript5的其它新特性

    之前两篇博客 ECMAScript5 Object的新属性方法,ECMAScript5 Array新增方法,分别介绍了ECMAScript5对Object和Array的拓展,这两个对象最常用,而且改动 ...

  7. 为什么eclipse中启动tomcat后,浏览器中出现404?

    问题描述: tomcat压缩包加压后,启动lib文件夹下面的startup.bat,在浏览器中输入http://localhost:8080/后出现熟悉的界面. 但是在eclipse中,jsp可以正常 ...

  8. 每天一个linux命令(53):route命令

    Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或 ...

  9. 知方可补不足~用CDC功能来对数据库变更进行捕捉

    回到目录 如果我们希望监视一个数据表的变化,在sql2008之前的版本里,在数据库端可能想到的只有触发器,或者在程序端通过监视自己的insert,update,delete来实现相应的功能,这种实现无 ...

  10. Atitit 如何利用先有索引项进行查询性能优化

    Atitit 如何利用先有索引项进行查询性能优化 1.1. 再分析的话就是我们所写的查询条件,其实大部分情况也无非以下几种:1 1.2. 范围查找 动态索引查找1 1.2.1. 索引联合 所谓的索引联 ...