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 (1≤i≤N1≤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 |hi−hj||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.
  • 2≤N≤1052≤N≤105
  • 1≤hi≤1041≤hi≤104

Input

Input is given from Standard Input in the following format:

NN
h1h1 h2h2 …… hNhN

Output

Print the minimum possible total cost incurred.


Sample Input 1 Copy

Copy
4
10 30 40 20

Sample Output 1 Copy

Copy
30

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


Sample Input 2 Copy

Copy
2
10 10

Sample Output 2 Copy

Copy
0

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


Sample Input 3 Copy

Copy
6
30 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 |30−60|+|60−60|+|60−50|=40|30−60|+|60−60|+|60−50|=40.

题目链接:https://atcoder.jp/contests/dp/tasks/dp_a

题意:给你一堆石头,每一个石头有一个高度,有一只青蛙站在第一个石头上,青蛙每一次可以跳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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#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<<dp[n];return 0;
}inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '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

atcoder A - Frog 1(DP)相关推荐

  1. 求三角形最大面积(DP)

    求三角形最大面积(DP) 在OJ上奇迹般WA了:WA:70. Why? #include <iostream> #include <string.h> using namesp ...

  2. LeetCode 编辑距离 II(DP)

    1. 题目 给你两个单词 s 和 t,请你计算出将 s 转换成 t 所使用的最少操作数. 你可以对一个单词进行如下两种操作: 删除一个字符 替换一个字符 注意: 不允许插入操作 题目保证有解 示例: ...

  3. LeetCode 1220. 统计元音字母序列的数目(DP)

    文章目录 1. 题目 2. 解题 1. 题目 给你一个整数 n,请你帮忙统计一下我们可以按下述规则形成多少个长度为 n 的字符串: - 字符串中的每个字符都应当是小写元音字母('a', 'e', 'i ...

  4. LeetCode 265. 粉刷房子 II(DP)

    文章目录 1. 题目 2. 解题 1. 题目 假如有一排房子,共 n 个,每个房子可以被粉刷成 k 种颜色中的一种,你需要粉刷所有的房子并且使其相邻的两个房子颜色不能相同. 当然,因为市场上不同颜色油 ...

  5. LeetCode 256. 粉刷房子(DP)

    文章目录 1. 题目 2. 解题 1. 题目 假如有一排房子,共 n 个,每个房子可以被粉刷成红色.蓝色或者绿色这三种颜色中的一种,你需要粉刷所有的房子并且使其与相邻的两个房子颜色不能相同. 当然,因 ...

  6. LeetCode 1223. 掷骰子模拟(DP)

    1. 题目 有一个骰子模拟器会每次投掷的时候生成一个 1 到 6 的随机数. 不过我们在使用它时有个约束,就是使得投掷骰子时,连续 掷出数字 i 的次数不能超过 rollMax[i](i 从 1 开始 ...

  7. LeetCode 1155. 掷骰子的N种方法(DP)

    1. 题目 这里有 d 个一样的骰子,每个骰子上都有 f 个面,分别标号为 1, 2, -, f. 我们约定:掷骰子的得到总点数为各骰子面朝上的数字的总和. 如果需要掷出的总点数为 target,请你 ...

  8. LeetCode 1139. 最大的以 1 为边界的正方形(DP)

    1. 题目 给你一个由若干 0 和 1 组成的二维网格 grid,请你找出边界全部由 1 组成的最大 正方形 子网格,并返回该子网格中的元素数量.如果不存在,则返回 0. 示例 1: 输入:grid ...

  9. 程序员面试金典 - 面试题 17.23. 最大黑方阵(DP)

    1. 题目 给定一个方阵,其中每个单元(像素)非黑即白. 设计一个算法,找出 4 条边皆为黑色像素的最大子方阵. 返回一个数组 [r, c, size] ,其中 r, c 分别代表子方阵左上角的行号和 ...

最新文章

  1. 使用Keras计算余弦相似度(Cosine Similarity)
  2. Java 使用匿名内部类在方法内部定义并启动线程
  3. σ(゚∀゚ ∬オレの性格だ
  4. c++中vector的用法详解
  5. Android数据存储——2.文件存储_C_DOM解析XML文档
  6. SDNU 1263.C语言程序设计教程(第三版)课后习题10.5(约瑟夫环)
  7. Project Reactor展开方法
  8. 清华大学《操作系统》(二十二):文件系统
  9. Solr Facet技术的应用与研究
  10. jvm相关參数,调优
  11. DDD(领域驱动设计)示例目录结构
  12. Python中print函数的使用
  13. wdatepicker不显示秒_「超逸酷玩」秒秒测智能健康日历如何正确使用电子墨水屏...
  14. Django__WSGI
  15. 华为服务器双系统教程,双系统安装教程
  16. 【网络】java密码安全
  17. USYD悉尼大学DATA1002 详细作业解析Module6
  18. 惜我者,我惜之; 冷我者,我弃之
  19. Centos桌面版无法打开Chrome浏览器
  20. Java问题——can not be represented as java.sql.Date 错误解决

热门文章

  1. 在一个类型的继承体系中,成员的构造顺序
  2. maven setting.xml 中文配置详解(全配置)
  3. Redis 初次尝试
  4. Vue项目代码改进(二)—— element-UI的消息显示时间修改
  5. 架构师不可不知的十大可扩展架构
  6. opengl微发展理解
  7. 深入理解闭包系列第二篇——从执行环境角度看闭包
  8. strcpy函数的实现
  9. 【linux】学习2
  10. JOSSO在JBOSS中安装与配置