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

Copy
5 3
10 30 40 50 20

Sample Output 1 Copy

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

Copy
3 1
10 20 10

Sample Output 2 Copy

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

Copy
2 100
10 10

Sample Output 3 Copy

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

Copy
10 4
40 10 20 70 80 10 20 70 80 60

Sample Output 4 Copy

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)的更多相关文章

  1. atcoder A - Frog 1(DP)

    A - Frog 1 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement There a ...

  2. Atcoder E - RGB Sequence(dp)

    题目链接:http://arc074.contest.atcoder.jp/tasks/arc074_c 题意:一共有3种颜色,红色,绿色,蓝色.给出m个要求l,r,x表示在区间[l,r]内要有x种不 ...

  3. Atcoder Beginner Contest 155E(DP)

    #definde HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; int main(){ ios: ...

  4. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  5. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  6. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  7. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  8. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  9. 【CF625E】Frog Fights(模拟)

    [CF625E]Frog Fights(模拟) 题面 CF 洛谷 翻译: 有\(n\)只青蛙在一个被分为了\(m\)等分的圆上,对于每份顺时针依次标号. 初始时每只青蛙所在的位置是\(p_i\),速度 ...

随机推荐

  1. ApplicationContext中Bean的生命周期

    引言 Bean应用上下文中的生命周期和在BeanFactory中生命周期类似,不同的是,如果Bean实现了org.springframework.context.ApplicationContextA ...

  2. 西安80投影坐标系转WGS84地理坐标系如何求七参数

    需求:西安80投影坐标系(平面坐标)转为WGS84地理坐标系(球面坐标) 这其中涉及的问题主要有以下两点: 1.一个是投影坐标系,一个是地理坐标系,而七参数指的是两个地理坐标系之间的转换,因此需要把投 ...

  3. 2018年值得关注的10大JavaScript动画库

    2018年值得关注的10大JavaScript动画库 旭日云中竹 前端早读课 1周前 前言 平时大家开发动画是采用什么方式呢?虽然18年过半,可这十个动画库是真的没听过几个,有点尴尬.今日早读文章由@ ...

  4. 控件_SeekBar与RatingBar

    这两种进度条都是ProgressBar的子类 SeekBar:是一种可以拖动的进度条,比如播放音乐的进度 import android.app.Activity; import android.os. ...

  5. c++のmap的遍历

    一.定义如:map < int, CString > 或者 map < int, 结构体名>的元素遍历 map < int, CString >  map;     ...

  6. 用requests爬取一个招聘网站

    import requestsimport re session = requests.session()第一步:访问登陆页,拿到X_Anti_Forge_Token,X_Anti_Forge_Cod ...

  7. Java没有头文件的原因

    http://bbs.csdn.net/topics/100134244 C/C++ 之所以需要头文件(.h),有两个用处,一个是在开发编译的时候,在各个编译单元(Compile Unit)之间共享同 ...

  8. Android中如何使用xmlns

    http://blog.csdn.net/lihenair/article/details/41009711 工作中时常需要自定义控件,除了按键,draw以外,还需要对控件属性进行一些初始化的操作,比 ...

  9. css权重 vs 浏览器渲染 -- css之弊病

    昨日,突现一个bug,令人十分恼火. 基本场景 自己实现一多选日历,可多选多天(相连或不相连均可)."贵司"的需求真心有些小复杂了,"市面"上没有这样的相似的东 ...

  10. 转载 互斥体与互锁 <第五篇>

    互斥体实现了“互相排斥”(mutual exclusion)同步的简单形式(所以名为互斥体(mutex)).互斥体禁止多个线程同时进入受保护的代码“临界区”.因此,在任意时刻,只有一个线程被允许进入这 ...