博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
atcoder A - Frog 1(DP)
阅读量:4308 次
发布时间:2019-06-06

本文共 2723 字,大约阅读时间需要 9 分钟。

A - Frog 1


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100100 points

Problem Statement

There are NN stones, numbered 1,2,,N1,2,…,N. For each ii (1iN1≤i≤N), the height of Stone ii is hihi.

There is a frog who is initially on Stone 11. He will repeat the following action some number of times to reach Stone NN:

  • If the frog is currently on Stone ii, jump to Stone i+1i+1 or Stone i+2i+2. Here, a cost of |hihj||hi−hj| is incurred, where jj is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone NN.

Constraints

  • All values in input are integers.
  • 2N1052≤N≤105
  • 1hi1041≤hi≤104

Input

Input is given from Standard Input in the following format:

NNh1h1 h2h2 …… hNhN

Output

Print the minimum possible total cost incurred.


Sample Input 1 Copy

Copy
410 30 40 20

Sample Output 1 Copy

Copy
30

If we follow the path 11 → 22 → 44, the total cost incurred would be |1030|+|3020|=30|10−30|+|30−20|=30.


Sample Input 2 Copy

Copy
210 10

Sample Output 2 Copy

Copy
0

If we follow the path 11 → 22, the total cost incurred would be |1010|=0|10−10|=0.


Sample Input 3 Copy

Copy
630 10 60 10 60 50

Sample Output 3 Copy

Copy
40

If we follow the path 11 → 33 → 55 → 66, the total cost incurred would be |3060|+|6060|+|6050|=40|30−60|+|60−60|+|60−50|=40.

 

题目链接:

题意:给你一堆石头,每一个石头有一个高度,有一只青蛙站在第一个石头上,青蛙每一次可以跳1-2个石头,并且产生起跳高度和落地高度的差的消耗。

问你青蛙跳到第N个石头,最小需要消耗多少能量?

 

思路:

简单的线性DP, 定义dp[i]的状态意义为青蛙跳到第i个石头的时候消耗的最小能量,

转移方程即为:dp[i]=min(dp[i-2]+abs(a[i]-a[i-2]),dp[i-1]+abs(a[i]-a[i-1]))

初始状态定义: dp[1] = 0 ,  dp[2]=| a[2]-a[1] |

dp[2]一定要预处理,状态转移只能从i=3开始,因为第二个石头只能由第一个石头跳过去。

不这样定义会wa的。(亲测,23333)

我的AC代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,x,n) for(int i=x;i
#define pll pair
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define MS0(X) memset((X), 0, sizeof((X)))#define MSC0(X) memset((X), '\0', sizeof((X)))#define pb push_back#define mp make_pair#define fi first#define se second#define gg(x) getInt(&x)using namespace std;typedef long long ll;inline void getInt(int* p);const int maxn=1000010;const int inf=0x3f3f3f3f;/*** TEMPLATE CODE * * STARTS HERE ***/ll n;ll dp[maxn];ll a[maxn];int main(){ gbtb; cin>>n; repd(i,1,n) { cin>>a[i]; } dp[1]=0; dp[0]=0; dp[2]=abs(a[2]-a[1]); repd(i,3,n) { dp[i]=min(dp[i-2]+abs(a[i]-a[i-2]),dp[i-1]+abs(a[i]-a[i-1])); } cout<
= '0' && ch <= '9') { *p = *p * 10 - ch + '0'; } } else { *p = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 + ch - '0'; } }}

 

转载于:https://www.cnblogs.com/qieqiemin/p/10247378.html

你可能感兴趣的文章
Python的编码和解码
查看>>
docker
查看>>
停车场系统安全岛设计施工要求
查看>>
Docker实战
查看>>
asp.net core结合Gitlab-CI实现自动化部署
查看>>
RDIFramework.NET ━ .NET快速信息化系统开发框架 V2.7 版本发布
查看>>
EasyNVR H5无插件摄像机直播解决方案前端解析之:关于直播页面和视频列表页面切换的问题...
查看>>
django搭建一个小型的服务器运维网站-拿来即用的bootstrap模板
查看>>
redis事务
查看>>
Java_基础语法之dowhile语句
查看>>
HDU 2175 汉诺塔IX
查看>>
PAT 甲级 1021 Deepest Root
查看>>
查找代码错误.java
查看>>
vc获取特殊路径(SpecialFolder)
查看>>
单例模式
查看>>
int(3)和int(11)区别
查看>>
201521123061 《Java程序设计》第十一周学习总结
查看>>
代码小思考
查看>>
Unity中的销毁方法
查看>>
ceph删除pool提示(you must first set the mon_allow_pool_delete config option to true)解决办法...
查看>>