dp or 贪心 --- hdu : Road Trip
来源:互联网 发布:用java编写游戏 编辑:程序博客网 时间:2024/05/21 15:43
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 32.00 71.50 81.00 350 61.50 204.20 51.15 351.41 271.92 302.21 150 0Sample Output
Journey 1: 29.00Journey 2: 117.64Problem SourceHNU 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=1;
while(scanf("%d %d",&v,&n),v+n)
{
double cost=0.0;
printf("Journey %d: ",kase++);
bool flag=0;
for(int i=1;i<=n;i++)
{
scanf("%lf %d",&node[i].p,&node[i].next);
if(node[i].next>v)
{
flag=1;
break;
}
}
if(flag==1)
{
puts("0.00");
continue;
}
int now=0;
for(int i=1;i<n;i++) //zui hou mei pan
{
if(i>1) // begin to No.2
{
if(node[i].p>node[i-1].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+1].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=0;
}
else
{
tmp=0;
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=0;
}
else
{
int tmp=node[n].next-now;
cost+=tmp*node[n].p;
now=0;
}
printf("%.2lf\n",cost);
}
return 0;
}
- dp or 贪心 --- hdu : Road Trip
- Road trip
- hdu 1257 最少拦截系统 dp or 贪心
- 贪心 ? OR DP?
- 12222 - Mountain Road(dp+贪心)
- 12222 - Mountain Road(dp+贪心)
- uva 12222 - Mountain Road(dp+贪心)
- POJ 2586 - 贪心 or DP
- hdu 1257 dp+贪心
- HDU 1257 DP + 贪心
- HDU 2577 DP || 贪心
- hdu 1257--dp/贪心
- hdu 5501 贪心+DP
- [DP/贪心] HDU 1257
- 【Hackerrank World11】Road Trip 题解
- spoj1435 - Vertex Cover(贪心or树形dp)
- POJ 2393 Yogurt factory(贪心 or DP)
- [HDU] 3660 Alice and Bob's Trip -- 树形DP?
- HashMap问答
- Tensorflow | Random |常用函数介绍
- 模拟 --- hdu 12878 : Fun With Fractions
- 数论
- 公众号基本信息介绍
- dp or 贪心 --- hdu : Road Trip
- Java--查看某一段代码或方法运行时间的方法
- 算法课第二周作业 | Majority Element
- 数据结构实验之栈一:进制转换(难道我学了个假栈?)
- Android Error Gradle sync failed 设备未就绪问题
- 暴力枚举 + 24点 --- hnu : Cracking the Safe
- 扫描线 + 线段树 : 求矩形面积的并 ---- hnu : 12884 Area Coverage
- 枚举 + 进制转换 --- hdu 4937 Lucky Number
- Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串