dp or 贪心 --- hdu : Road Trip
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). |
Input |
Input will consist of specifications for a series of journeys. Information |
Output |
Output should consist of one line for each journey comprising the journey |
Sample Input |
10 3 |
Sample Output |
Journey 1: 29.00 |
Problem Source |
HNU Contest |
Mean:
你要开车去n个小镇拜访你的老朋友,但是你不是高富帅,只得将油钱尽量的省,现在给你油箱的容量、小镇数量、每个小镇的油价、到达下一个小镇需要的油,你可以买油也可卖油,问你最少的油钱是多少。
analyse:
这题就是一个贪心的思想,如果下一站的油价比这站高,那么别想了,将油箱装满,到下一站就可以赚钱;反之就只需买够路上用的就可。
当然有人说这题是dp,这也没错,看个人怎么理解,其实这题就两个状态之间转移,dp已经退化为贪心。
Time complexity:O(n)
Source code:
// 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的更多相关文章
- P1095 守望者的逃离——DP?贪心?
https://www.luogu.org/problem/P1095 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大 ...
- P2279 消防局的设立 (树形DP or 贪心)
(点击此处查看原题) 树形DP写法 看到这个题的要求,很容易相到这是一个树形DP的问题,但是dp数组应该如何设计并转移才是关键 dp[i][0]代表当前结点可以向上覆盖2层,自身一定被覆盖dp[i][ ...
- hdu 1257 && hdu 1789(简单DP或贪心)
第一题;http://acm.hdu.edu.cn/showproblem.php?pid=1257 贪心与dp傻傻分不清楚,把每一个系统的最小值存起来比较 #include<cstdio> ...
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- URAL 1203 Scientific Conference dp?贪心
题目:click here 分明就是贪心怎么会在dp的专题 #include <bits/stdc++.h> using namespace std; typedef unsigned l ...
- luoguP1281 书的复制 DP,贪心
luoguP1281 书的复制 链接 https://www.luogu.org/problemnew/show/P1281 思路 简单dp,输出方案. 很明显dp记录路径对不对? 恭喜你死了. 求出 ...
- CF101D Castle 树形DP、贪心
题目传送门 题意:给出一个有$N$个点的树,你最开始在$1$号点,经过第$i$条边需要花费$w_i$的时间.每条边只能被经过$2$次.求出到达除$1$号点外所有点的最早时间的最小平均值.$N \leq ...
- [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 ...
- 2018.08.29 NOIP模拟 movie(状压dp/随机化贪心)
[描述] 小石头喜欢看电影,选择有 N 部电影可供选择,每一部电影会在一天的不同时段播 放.他希望连续看 L 分钟的电影.因为电影院是他家开的,所以他可以在一部电影播放过程中任何时间进入或退出,当然他 ...
随机推荐
- 【读书笔记】-- JavaScript数组
数组是一段线性分配的内存,它通过整数计算偏移并访问其中的元素.大多数的语言都会要求一个数组的元素是相同类型,但JavaScript数组可以包含任意类型. var misc = ['string', n ...
- 【吉光片羽】奇怪的Bug-细节的问题
这几天用Winform开发了一个小界面,遇到几个奇怪的问题,记录一下. 1.背景图片漏光. 当时很是奇怪,以为是图片的问题,让美工重新发,改成jpg也都存在.很是奇怪,原图这个地方肉眼看是不透明的,而 ...
- 一天一小段js代码(no.1)
10000个数字中缺少三个数,编程找出缺少的三个数字. 算法实现: /*生成10000个数中随机抽掉三个数后的数组*/ function supplyRandomArray(){ /*生成含有1000 ...
- Linux Shell变量
200 ? "200px" : this.width)!important;} --> 介绍 变量是shell脚本中必不可少的组成部分,在脚本中使用变量不需要提前声明.在ba ...
- 自制Unity小游戏TankHero-2D(2)制作敌方坦克
自制Unity小游戏TankHero-2D(2)制作敌方坦克 我在做这样一个坦克游戏,是仿照(http://game.kid.qq.com/a/20140221/028931.htm)这个游戏制作的. ...
- Java-继承 共3题
一.实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty和Staff.具体要求如下: (1)Person类中的属性有:姓名name(String类型),地址 ...
- How Google TestsSoftware - Part Three
Lots of questions in thecomments to the last two posts. I am not ignoring them. Hopefully many of th ...
- javaweb回顾第十一篇过滤器(附实现中文乱码问题)
1:过滤器概念 过滤器就是一种在请求目标资源的中间组件,比喻把污水转换成纯净水中间需要一个污水净化设备,那么这个设备就好比一个过滤器.那么我用图来表示过滤器(可以有多个过滤器)运行的过程 2:Filt ...
- Netty中的坑(下篇)
其实这篇应该叫Netty实践,但是为了与前一篇名字保持一致,所以还是用一下坑这个名字吧. Netty是高性能Java NIO网络框架,在很多开源系统里都有她的身影,而在绝大多数互联网公司所实施的服务化 ...
- 哈夫曼树(二)之 C++详解
上一章介绍了哈夫曼树的基本概念,并通过C语言实现了哈夫曼树.本章是哈夫曼树的C++实现. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载 ...