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\),速度 ...
随机推荐
- [经验总结] 在 windows 命令窗口中运行 python 脚本时提示 ModuleNotFoundError: No module named 'xxx'
先给出的代码和目录结构 获取CPU代码如下: # -*- coding:utf-8 -*- ''' Created on Sep 10, 2018 @author: ''' import sys im ...
- Django之Template
模板层(template) 概念: 模板与html的区别: 模板=html+模板语法 模板语法: 1 变量: {{}} 深度查询: 通过句点符. 列表,字典 clas ...
- centos7下安装docker(12.4容器如何与外部进行通信)
1.容器如何访问外部 前面我们做了很多试验:只要host能连外网,使用默认bridge(docker0)创建得容器就能访问外网,那么容器是怎样访问外网的呢? 注:这里的外网不仅是internet,包括 ...
- centos7下安装docker(3.2创建镜像build)
通过Dockerfile创建镜像 注:这个Dockerfile一开始真的不知道是在哪来的,还以为是在官网下载下来得(当然网上也有很多dockerfile的模板,参考:https://hub.docke ...
- 倒水问题(Fill, UVa 10603)
[题目描述] 有三个没有刻度的水壶,容量分别为a,b和c(单位为升,都是<=200的正整数).初始时前两个水壶是空的,而第三个装满了水.每次可以从一个水壶往一个水壶里倒水,直到一个水壶倒空或者另 ...
- 谈谈maven多模块
记得在校的时候,通常用的比较多是动态web工程,动态web工程导入最多就是jar包,这些jar包需要自己手动复制粘贴放入lib目录下,然后build path,有的IDE会自动build path有的 ...
- <转>聊聊持续集成
从别处看到了一篇关于持续集成的文章,个人感觉蛮不错的,分享给大家... 原文链接:对于持续集成实践的常见问题解答 1.什么是持续集成? 集成,就是一些孤立的事物或元素通过某种方式集中在一起,产生联系, ...
- Linux 安装erlang
安装rabbitmq的基础erlang1. 下载erlang 版本:otp_src_20.1.tar.gz 地址: http://erlang.org/download/?M=D 2.安装erlang ...
- Apache cxf暴露接口以及客户端调用之WebService初步理解
在我们真实的项目中,经常会调用别人提供给我们的接口,或者在自己的团队中, restful风格的前后端分离也经常会提供一个后端接口暴露出去供app,或者.net/C/C++程序员去调用,此时就需要使用到 ...
- 虚拟机vbox
https://www.virtualbox.org/wiki/Linux_Downloads 一直在报内核出错的问题,我尝试了各种方法还是无果,猜测是版本的问题,这里推荐各位安装virtualbox ...