B - Frog 2


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : <var>100100</var> points

Problem Statement

There are <var>NN</var> stones, numbered <var>1,2,,N1,2,…,N</var>. For each <var>ii</var> (<var>1iN1≤i≤N</var>), the height of Stone <var>ii</var> is <var>hihi</var>.

There is a frog who is initially on Stone <var>11</var>. He will repeat the following action some number of times to reach Stone <var>NN</var>:

  • If the frog is currently on Stone <var>ii</var>, jump to one of the following: Stone <var>i+1,i+2,,i+Ki+1,i+2,…,i+K</var>. Here, a cost of <var>|hihj||hi−hj|</var> is incurred, where <var>jj</var> is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone <var>NN</var>.

Constraints

  • All values in input are integers.
  • <var>2N1052≤N≤105</var>
  • <var>1K1001≤K≤100</var>
  • <var>1hi1041≤hi≤104</var>

Input

Input is given from Standard Input in the following format:

<var>NN</var> <var>KK</var>
<var>h1h1</var> <var>h2h2</var> <var></var> <var>hNhN</var>

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 <var>11</var> → <var>22</var> → <var>55</var>, the total cost incurred would be <var>|1030|+|3020|=30|10−30|+|30−20|=30</var>.


Sample Input 2 Copy

Copy
3 1
10 20 10

Sample Output 2 Copy

Copy
20

If we follow the path <var>11</var> → <var>22</var> → <var>33</var>, the total cost incurred would be <var>|1020|+|2010|=20|10−20|+|20−10|=20</var>.


Sample Input 3 Copy

Copy
2 100
10 10

Sample Output 3 Copy

Copy
0

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


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 <var>11</var> → <var>44</var> → <var>88</var> → <var>1010</var>, the total cost incurred would be <var>|4070|+|7070|+|7060|=40|40−70|+|70−70|+|70−60|=40</var>.

 

题目链接: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=1000010;
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,1,n)
    {
        cin>>a[i];
    }
    dp[1]=0;
    dp[0]=0;
    repd(i,2,n)
    {
        dp[i]=inf;
    }
    repd(i,2,n)
    {
        for(int j=i-1;j>=max(1ll,i-k);j--)
        {
            dp[i]=min(dp[i],dp[j]+abs(a[i]-a[j]));
        }
    }
    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';
        }
    }
}