atcoder B - Frog 2 (DP)
B - Frog 2
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 one of the following: Stone i+1,i+2,…,i+Ki+1,i+2,…,i+K. 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≤K≤1001≤K≤100
- 1≤hi≤1041≤hi≤104
Input
Input is given from Standard Input in the following format:
NN KK
h1h1 h2h2 …… hNhN
Output
Print the minimum possible total cost incurred.
Sample Input 1 Copy
5 3
10 30 40 50 20
Sample Output 1 Copy
30
If we follow the path 11 → 22 → 55, the total cost incurred would be |10−30|+|30−20|=30|10−30|+|30−20|=30.
Sample Input 2 Copy
3 1
10 20 10
Sample Output 2 Copy
20
If we follow the path 11 → 22 → 33, the total cost incurred would be |10−20|+|20−10|=20|10−20|+|20−10|=20.
Sample Input 3 Copy
2 100
10 10
Sample Output 3 Copy
0
If we follow the path 11 → 22, the total cost incurred would be |10−10|=0|10−10|=0.
Sample Input 4 Copy
10 4
40 10 20 70 80 10 20 70 80 60
Sample Output 4 Copy
40
If we follow the path 11 → 44 → 88 → 1010, the total cost incurred would be |40−70|+|70−70|+|70−60|=40|40−70|+|70−70|+|70−60|=40.
题目链接:https://atcoder.jp/contests/dp/tasks/dp_b
题意:这一篇的进阶版。
把每次只能跳1~2步改成1~k步。
做法只需要其他的不变,把转移方程那里循环1~k次就行了。
附上代码:
#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];
ll k;
int main()
{
gbtb;
cin>>n>>k;
repd(i,,n)
{
cin>>a[i];
}
dp[]=;
dp[]=;
repd(i,,n)
{
dp[i]=inf;
}
repd(i,,n)
{
for(int j=i-;j>=max(1ll,i-k);j--)
{
dp[i]=min(dp[i],dp[j]+abs(a[i]-a[j]));
}
}
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 B - Frog 2 (DP)的更多相关文章
- atcoder A - Frog 1(DP)
A - Frog 1 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\),速度 ...
随机推荐
- lamp/lnmp下添加PHP扩展
在linux下安装好基本的PHP运行环境后有时候添加了新的功能,就得增加新的扩展,比如之前没有安装redis扩展,可以手动编译安装相关的扩展可以找下下载的php源码包中,ext目录下是否有相关的扩展源 ...
- React框架简介
React的基本认识 Facebook开源的一个js库,一个用来动态构建用户界面的js库 英文官网,中文官网 React的特点 Declarative(声明式编码),Component-Based(组 ...
- PC端和移动APP端CSS样式初始化
CSS样式初始化分为PC端和移动APP端 1.PC端:使用Normalize.css Normalize.css是一种CSS reset的替代方案. 我们创造normalize.css有下面这几个目的 ...
- django -- 推荐商品算法
协同过滤算法之基于物品的推荐算法 目前有关个性化推荐算法主要分为三大类:1.基于协同过滤的推荐:2.基于内容过滤的推荐和3.社会化推荐. 本文主要讨论基于协同过滤的推荐,而该算法也可以划分为两类: 1 ...
- HashMap的扩容机制---resize()
虽然在hashmap的原理里面有这段,但是这个单独拿出来讲rehash或者resize()也是极好的. 什么时候扩容:当向容器添加元素的时候,会判断当前容器的元素个数,如果大于等于阈值---即当前数组 ...
- Intel CPU 漏洞分析
Intel CPU漏洞分析报告 预备知识 存储分级 由于计算机存储分级的特性(第一级:寄存器,第二级:高速缓存,第三级:内存,第四级:磁盘),每一级之间的访问速度差距高达数量级.所以处理器会将用到的数 ...
- Objective-C ARC下IBOutlet属性是用weak还是strong来修饰
1.苹果官方说明: From a practical perspective, in iOS and OS X outlets should be defined as declared proper ...
- ESP32 I2S
I2S支持DMA; I2S可以直接利用DAC来输出模拟信号 (GPIO 25 & GPIO 26): 高精度时钟使能参数: .use_apll = true ESP32配置外设一般都是配置 ...
- http协议进阶(六)代理
web代理服务器是网络的中间实体,位于客户端和服务器之间,扮演“中间人”的角色,作用是在各端点之间来回传送报文. 其原理是:客户端向代理服务器发送请求报文,代理服务器正确的处理请求和连接,然后返回响应 ...
- linux 资料
吾爱linux 摘自传智播客