A

Problem Statement

There are NN stones, numbered 1,2,,N1,2,…,N. For each ii (1iN1≤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 |hihj||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.
  • 2N1052≤N≤105
  • 1hi1041≤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

Copy
4
10 30 40 20

Sample Output 1 Copy

Copy
30

If we follow the path 11 → 22 → 44, the total cost incurred would be |1030|+|3020|=30|10−30|+|30−20|=30.


Sample Input 2 Copy

Copy
2
10 10

Sample Output 2 Copy

Copy
0

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


Sample Input 3 Copy

Copy
6
30 10 60 10 60 50

Sample Output 3 Copy

Copy
40

If we follow the path 11 → 33 → 55 → 66, the total cost incurred would be |3060|+|6060|+|6050|=40|30−60|+|60−60|+|60−50|=40.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
using namespace std;
int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const int mod =1e9+7;
const int N = 1e5+5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m * n / gcd(m, n);
}
bool prime(int x) {
    if (x < 2) return false;
    for (int i = 2; i * i <= x; ++i) {
        if (x % i == 0) return false;
    }
    return true;
}
ll qpow(ll m, ll k, ll mod)
{
    ll res = 1, t = m;
    while (k)
    {
        if (k & 1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}      

int main()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    rep(i, 1, n)
        cin >> a[i];
    vector<int> dp(n + 1,0);
    a[0] = a[1];
    rep(i, 2, n)
    {
        dp[i] = min(dp[i-1]+abs(a[i] - a[i - 1]),dp[i-2] + abs(a[i] - a[i - 2]));
    }
    cout << dp[n] << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/dealer/p/13129086.html