atcoder A - Frog 1(DP)
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
4
10 30 40 20
Sample Output 1 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
2
10 10
Sample Output 2 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
6
30 10 60 10 60 50
Sample Output 3 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=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n;
ll dp[maxn];
ll a[maxn];
int main()
{
gbtb;
cin>>n;
repd(i,,n)
{
cin>>a[i];
}
dp[]=;
dp[]=;
dp[]=abs(a[]-a[]);
repd(i,,n)
{
dp[i]=min(dp[i-]+abs(a[i]-a[i-]),dp[i-]+abs(a[i]-a[i-])); }
cout<<dp[n];
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
atcoder A - Frog 1(DP)的更多相关文章
- atcoder B - Frog 2 (DP)
B - Frog 2 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement There a ...
- Atcoder E - RGB Sequence(dp)
题目链接:http://arc074.contest.atcoder.jp/tasks/arc074_c 题意:一共有3种颜色,红色,绿色,蓝色.给出m个要求l,r,x表示在区间[l,r]内要有x种不 ...
- Atcoder Beginner Contest 155E(DP)
#definde HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; int main(){ ios: ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- 【CF625E】Frog Fights(模拟)
[CF625E]Frog Fights(模拟) 题面 CF 洛谷 翻译: 有\(n\)只青蛙在一个被分为了\(m\)等分的圆上,对于每份顺时针依次标号. 初始时每只青蛙所在的位置是\(p_i\),速度 ...
随机推荐
- MySQL表名区分大小写设置
关闭MySQL服务 在服务运行目录找到my.ini或者my.cnf文件 打开文件,找到[mysqld]在下面增加一行 lower_case_table_names=0 (0:大小写敏感;1:大小写不敏 ...
- div放在li标签中,无法撑开li标签的问题
作为一个前端菜鸟,我又碰到问题了,今天把div放到li标签中,发现div并没有把li标签撑开,而是在li标签边界之外,具体情况如下图所示: 那么,怎样才能达到预期的效果(每个li中放置一个div标签, ...
- Python解析器
当我们编写Python代码时,我们得到的是一个包含Python代码的以.py为扩展名的文本文件.要运行代码,就需要Python解释器去执行.py文件. 由于整个Python语言从规范到解释器都是开源的 ...
- 转 一个web项目web.xml的配置中<context-param>配置作用
一个web项目web.xml的配置中<context-param>配置作用 <context-param>的作用:web.xml的配置中<context-param& ...
- [转]Ubuntu 常用解压与压缩命令
.tar 文件(注:tar是打包,不是压缩!) # 仅打包,并非压缩 tar -xvf FileName.tar # 解包 tar -cvf FileName.tar DirName # 将DirNa ...
- 查看和指定SpringBoot内嵌Tomcat的版本
查看当前使用的Tomcat版本号 Maven Repository中查看 比如我们需要查Spring Boot 2.1.4-RELEASE的内嵌Tomcat版本, 可以打开链接: https://mv ...
- Photoshop 基础四 填充(渐变、油漆桶)
一 渐变 二 油漆桶 一 渐变 技术点:选框工具(矩形.圆形.选区.反选).变换(透视).渐变 1)新建图层——>矩形选框工具,画一个矩形 2)渐变工具,定义渐变颜色 3)编辑——>变换— ...
- eclipse 报错Version 1.6.0_45 of the JVM is not suitable for this product. Version:1.7 or greater is required
最近离职来了一家新公司,之前的公司的开发IDE用的是IntelliJIDEA和SpringSourceToolSuit,自己在家里用的也是MyEclipse,所以使用eclipse的经验还是不足.结果 ...
- odoo学习之带出信息
# 输入客户带出它默认的发运方式和包装方式 def on_change_partner_id_return(self,cr,uid,ids,partner_id,context=None): resu ...
- 从一些代码方法中,去学习C#委托
先来看看下面一个类中的一些方法: class Bc { public double Add(double number1, double number2) { return number1 + num ...