题意:
给定一个序列,以最小代价将其变成单调不增或单调不减序列,这里的代价看题目公式。
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>
#include <stdlib.h>
#include <stdio.h>
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define l(x) (x<<1)
#define r(x) (x<<1|1)
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
int n, a[2222], c[2222], len, dp[2222][2222],mmin,ans;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
c[i] = a[i];
}
sort(c + 1, c + n + 1);
len = unique(c + 1, c + n + 1) - c - 1;
for (int i = 1; i <= n; i++) {
mmin = INF;
for (int j = 1; j <= len; j++) {
mmin = min(mmin, dp[i - 1][j]);
dp[i][j] = mmin + abs(a[i] - c[j]);
}
}
ans = INF;
for (int i = 1; i <= len; i++)
ans = min(dp[n][i], ans);
cout << ans << endl;
return 0;
}