51nod1270 【dp】

思路:
dp[i][0]代表第i个位置取1,dp[i][1]代表第i个位置取b[i]。

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N=5e4+10;


LL dp[N][2];
LL a[N];
int main()
{
    LL n;
    scanf("%lld", &n);
    for(LL i = 1; i <= n; ++i)
        scanf("%lld", &a[i]);
    for(LL i = 2; i <= n; ++i)
    {
        dp[i][0] = max(dp[i - 1][1] + abs(1 - a[i - 1]), dp[i - 1][0]);
        dp[i][1] = max(dp[i - 1][1] + abs(a[i] - a[i - 1]), dp[i - 1][0]+abs(a[i] - 1));
    }
    printf("%lld\n", max(dp[n][0], dp[n][1]));
    return 0;
}


posted @ 2016-09-08 16:24  see_you_later  阅读(85)  评论(0编辑  收藏  举报