题意:给定一个序列b[],初始序列为a[] = {0}。每次操作可以任意选择一个i,使得a[i] a[i+1] ... a[n]减一或者加一。问最少需要多少次操作能够使序列a[]变为序列b[]。
思路:一开始dp写了一发WA了。。。最后直接贪心模拟就AC了。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-4
#define MAXN (200000+10)
#define MAXM (1000000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 100000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
LL a[MAXN];
int main()
{
int n; Ri(n);
LL ans = 0; a[0] = 0;
for(int i = 1; i <= n; i++)
{
Rl(a[i]);
ans += abs(a[i] - a[i-1]);
}
Pl(ans);
return 0;
}