题目链接

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4920

problem  description

Two years ago, Putri bought an electric bike (e-bike). She likes e-bike a lot since it can assist her in cycling through steep roads when commuting to work. Over time, the battery capacity decreases. Now, her e-bike battery capacity is so low that she needs to carefully plan when to turn on the assist and at which level; different level of assist consumes different amount of energy and produces different assist power.

Putri divides the road that she travels from her home to her workplace into N segments where each segment has a steepness level. For example, the figure below shows a road with N = 7 segments.

Segment     #1 #2 #3 #4 #5 #6 #7

Steepness    10   3   0   1   2  15   9

From her home, Putri has to travel from the first road segment, to the second road segment, and so on, until the last segment to reach her work place. In the example above, the first segment has steepness of 10 which means Putri has to pedal her bike with 10 unit of energy. Her e-bike has fixed 4 assist levels where each level generates different power, as shown in the following table:

     Assist Level   0  1  2  3

Assist Power  0  4  8  11

Assist level L will consume L unit of energy from the battery and will give Putri additional pedaling assist power according to the table above. Putri can only change the assist level to level L if the battery has at least L energy left and she is at the beginning of a road segment. Setting an assist level where the generated power is higher than the road steepness will cause the excess energy power to be wasted.

For example, if Putri sets the assist level L = 2 at the beginning of the first road segment (with steepness 10), then she only needs to pedal her bike with 2 unit of energy instead of 10 (since her ebike is assisting her with 8 unit of energy) to advance to the second road segment. If Putri sets the assist level L = 3, her e-bike generates more power to handle the steepness 10, thus she does not need to pedal at all, and the excess energy is wasted.

Putri can change the assist level instantly before entering a road segment, however, she does not want to change the assist level more than K times (it’s too tiring). If there is not enough energy in the battery to support the selected assist level for the road segment, the e-bike will shutdown at the beginning of the road segment and Putri has to pedal through the rest of the road segments, i.e. the assist level automatically set to 0 for the rest of the journey. Note that Putri can change her e-bike assist level (given it’s still less than K) at the beginning of the road segment to avoid shutdown by force. Initially at her home, the assist level is set to 0 and the battery is fully charged with E unit of energy. Putri wants to know the minimum energy she will need to pedal the bike to reach the workplace if she utilizes her e-bike optimally.

Input

The first line of input contains T (T ≤ 100) denoting the number of cases. Each case begins with three integers: N, K, and E in a line (1 ≤ N ≤ 1, 000; 0 ≤ K ≤ 10; 0 ≤ E ≤ 50) as described in the problem statement above. The next line contains N non-negative integers denoting the steepness level of i-th segment where i = 1 . . . N respectively. The steepness level of any road segment is at most 15.

Output

For each case, output ‘Case #X: Y ’, where X is the case number starts from 1 and Y is the minimum energy Putri needs to pedal the e-bike from her home to her workplace.

Explanation for 1st sample case:

Putri changes the assist level to 1 at (the beginning of) road segment #2, then change the assist level to 3 at road segment #6. Thus, she needs to pedal with 10 unit of energy for road segment #1 and 4 unit of energy for road segment #6. Note that if she changes the assist level to 1 at road segment #1 and then to assist level 3 at road segment #6, then at the beginning of road segment #7 the battery only has 2 unit of energy left and will automatically shutdown to assist level 0, thus Putri has to pedal with 9 energy for road segment #7.

Explanation for 2nd sample case:

Putri changes the assist level to 3 at road segment #1 and then changes to assist level 2 at road segment #2. Thus, she only needs to pedal 7 unit of energy for road segment #6 and 1 unit of energy for road segment #7.

Explanation for 3rd sample case:

Putri changes the assist level to 3 at road segment #1, then changes to assist level 1 at road segment #2, finally changes to assist level 3 at road segment #6. Thus, she only needs to pedal 4 unit of energy for road segment #6.

Sample Input

5

7 2 10

10 3 0 1 2 15 9

7 2 15

10 3 0 1 2 15 9

7 3 15

10 3 0 1 2 15 9

5 2 5

11 15 1 14 12

15 8 30

2 14 6 1 2 13 14 12 13 12 7 12 1 2 10

Sample Output

Case #1: 14

Case #2: 8

Case #3: 4

Case #4: 34

Case #5: 18

题意:一个人骑电动车去公司,电动车的初始电能是E,电动车有四个档位,0、1、2、3分别对应的输出能量是0、4、8、11 消耗的电能是对应的档位值大小。现在有n段路,每段路需要提供cost[i]的能量才能走过,如果在某一段路的档位对应的输出能量小于cost[i],那么剩余的由这个人提供,如果电动车输出能量大于cost[i],则多余部分浪费掉。现在电动车初始档位为0,求通过这条路且换挡不超过K次的情况下人提供的最小能量,注意:在某一段路时电动车的剩余电能小于当前的档位时,电动车报废,接下来的路由人提供能量;

思路:定义dp[i][j][k][f] 表示走到第i段路换了j次档电动车消耗k的电能,当前档位为f是人所消耗的能量;

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int inf=0x3f3f3f3f;
int dp[][][][];
int d[]={,,,};
int cost[]; int main()
{
int T,Case=,N,K,E;
cin>>T;
while(T--)
{
int ans=inf;
scanf("%d%d%d",&N,&K,&E);
for(int i=;i<=N;i++)
scanf("%d",&cost[i]);
memset(dp,inf,sizeof(dp));
dp[][][][]=;
for(int i=;i<=N;i++)
for(int j=;j<=K;j++)
for(int k=;k<=E;k++)
for(int s=;s<;s++)
{
if(k>=s)
for(int p=;p<;p++)
{
if(s==p) dp[i][j][k][s]=min(dp[i][j][k][s],dp[i-][j][k-s][p]+max(,cost[i]-d[s]));
else if(j>) dp[i][j][k][s]=min(dp[i][j][k][s],dp[i-][j-][k-s][p]+max(,cost[i]-d[s]));
if(j==K&&s==) dp[i][j][k][]=min(dp[i][j][k][],dp[i-][j][k][p]+cost[i]);
if(i==N) ans=min(ans,dp[i][j][k][s]);
}
}
printf("Case #%d: %d\n",Case++,ans);
}
return ;
}

还可以深搜:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <map>
#include <bitset>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
int N,K,E;
int a[],sum[],p[]={,,,};
int dp[][][][];
bool vis[][][][]; int calc(int pos,int k,int e,int f)
{
if(pos==N+) { return ; }
if(vis[pos][k][e][f]) return dp[pos][k][e][f];
int Sum;
if(e==){
Sum=sum[N]-sum[pos-];
vis[pos][k][e][f]=;
dp[pos][k][e][f]=Sum;
return Sum;
} int tmp=inf;
for(int i=;i<;i++)
{
if(f!=i&&e>=i&&k>){
k--; e-=i; pos++;
Sum=calc(pos,k,e,i)+max(,a[pos-]-p[i]);
tmp=min(tmp,Sum);
k++; e+=i; pos--;
}
else if(f==i&&e>=i){
e-=i; pos++;
Sum=calc(pos,k,e,f)+max(,a[pos-]-p[i]);
tmp=min(tmp,Sum);
e+=i; pos--;
}
else if(k==&&e<f){
Sum=sum[N]-sum[pos-];
vis[pos][k][e][f]=;
dp[pos][k][e][f]=Sum;
return Sum;
}
}
vis[pos][k][e][f]=;
dp[pos][k][e][f]=tmp;
return tmp;
}
int main()
{
int T,Case=;
cin>>T;
while(T--)
{
sum[]=;
scanf("%d%d%d",&N,&K,&E);
for(int i=;i<=N;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
}
memset(vis,,sizeof(vis));
int u=calc(,K,E,);
printf("Case #%d: %d\n",Case++,u);
}
return ;
}

UVALive 6908---Electric Bike(DP或记录型深搜)的更多相关文章

  1. UVALive 6908 Electric Bike dp

    Electric Bike 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8 ...

  2. DP学习记录Ⅰ

    DP学习记录Ⅱ 前言 状态定义,转移方程,边界处理,这三部分想好了,就问题不大了.重点在状态定义,转移方程是基于状态定义的,边界处理是方便转移方程的开始的.因此最好先在纸上写出自己状态的意义,越详细越 ...

  3. DP学习记录Ⅱ

    DP学习记录Ⅰ 以下为 DP 的优化. 人脑优化DP P5664 Emiya 家今天的饭 正难则反.考虑计算不合法方案.一个方案不合法一定存在一个主食,使得该主食在多于一半的方法中出现. 枚举这个&q ...

  4. [IOI1999]花店橱窗布置(DP路径记录)

    题目:[IOI1999]花店橱窗布置 问题编号:496 题目描述 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序编号,V ...

  5. poj1015陪审团——DP+路径记录

    题目:http://poj.org/problem?id=1015 DP的第一维是选了几个人,第二维是当前D与P的差值,而值存的是当前D与P的和: 技巧1:通过平移避免负角标,即代码中的fix: 技巧 ...

  6. Codeforces 453B Little Pony and Harmony Chest:状压dp【记录转移路径】

    题目链接:http://codeforces.com/problemset/problem/453/B 题意: 给你一个长度为n的数列a,让你构造一个长度为n的数列b. 在保证b中任意两数gcd都为1 ...

  7. UVALive 6430 (水dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. HDU 2296 Ring ( Trie图 && DP && DP状态记录)

    题意 : 给出 m 个单词,每一个单词有一个权重,如果一个字符串包含了这些单词,那么意味着这个字符串拥有了其权重,问你构成长度为 n 且权重最大的字符串是什么 ( 若有权重相同的,则输出最短且字典序最 ...

  9. UVALive - 6952 Cent Savings dp

    题目链接: http://acm.hust.edu.cn/vjudge/problem/116998 Cent Savings Time Limit: 3000MS 问题描述 To host a re ...

随机推荐

  1. 《App研发录》 源码

    第1章源码: 1.1 重新规划Android项目结构 1.1.zip 1.2 为Activity定义新的生命周期 1.2.zip 1.3 统一事件编程模型 1.3.zip 1.4 实体化编程 1.4. ...

  2. 每天一个linux命令(45):free 命令

    free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 1.命令格式: free [参 ...

  3. hibernate用注解替代映射文件

    1.首先把原来的映射文件删掉,给实体类添加注解: @Entity //声明当前类为hibernate映射到数据库中的实体类 @Table(name="news") //声明tabl ...

  4. Java_IO流_File类配合使用(其中用到了递归)

    第一:Java File类的功能非常强大,利用Java基本上可以对文件进行所有的操作.以下对Java File文件操作以及常用方法进行简单介绍 案例1:遍历出指定目录下的文件夹,并输出文件名 stat ...

  5. UpdateException-更新条目时出错分析

    不屁话1张图搞定: 我是这个错: 2015-03-27 00:25:00,493 [9] ERROR log - System.Data.Entity.Infrastructure.DbUpdateE ...

  6. 进程管理三大扩展工具htop

    三大进程管理监控工具 HTOP 介绍: Htop是一款运行于Linux系统监控与进程管理软件,htop提供所有进程的列表,并且使用彩色标识出处理器.swap和内存状态.用户一般可以在top无法提供详尽 ...

  7. Windows phone 全景视图

    Windows phone 全景视图下为了实现可以上下滑动需要使用listbox. 需要的布局什么的,在listbox中填写 <ListBox Name="ListBox_new&qu ...

  8. java静态代理与动态代理

    原文链接:http://www.orlion.ga/207/ 一.代理模式 代理模式是经常用到的设计模式,代理模式是给指定对象提供代理对象.由代理对象来控制具体对象的引用. 代理模式涉及到的角色: 抽 ...

  9. ASP.NET MVC5 网站开发实践(二) Member区域 - 用户部分(1)用户注册

    上一次把基本框架搭建起来了,这次开始整Web部分,终于可以看到界面了小激动一下.web项目部分从用户功能开始,基本有注册,登录.注销.查找.查看.删除等涉及Member区域和Manage区域. 目录: ...

  10. 前端学PHP之错误处理

    × 目录 [1]错误报告 [2]错误级别 [3]错误处理[4]自定义错误[5]错误日志[6]异常处理[7]自定义异常 前面的话 错误处理对于程序开发至关重要,不能提前预测到可能发生的错误,不能提前采取 ...