Topic Link: [POJ 3666] Making the GRADE[DP]
The analysis:
A sequence of n numbers that allows any number of numbers to be added or reduced in a sequence, asking the minimum number of changes to allow the original sequence to become monotonically increasing or monotonically decreasing.
Ideas for solving problems:
For a number modification, we modify it to be the best choice for some of the numbers in the sequence (I can't understand it by the counter example). So we can just copy these numbers out of the sequence, and there's the next:
Set state Dp[i][j] to the number of I, with b[j] as the minimum value for the end change. Then there is the transfer equation Dp[i][j]=min (dp[i−1][k]) +|a[i]−b[j]| (k=0∼j) Dp[i][j] = min (dp[i-1][k]) + |a[i]-b[j]| (K=0\sim j) This equation is three-party, for this problem will obviously t. Note that min (Dp[i-1][k]) is the minimum value of the first J, so maintain the minimum value at the time of the transfer. This can reduce the complexity of the square.
Personal experience:
The first n sum, the minimum value, etc., can be so optimized, 2333
The specific code is as follows:
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring
> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set>
#include <sstream> #include <stack> #include <string> #define LL-long using namespace std;
const int INF = 0x7f7f7f7f;
const int MAXN = 2e3 + 111;
int A[MAXN], B[MAXN];
int DP[MAXN][MAXN];
BOOL CMP (int a, int b) {return a > B;}
int main () {int n; cin >> N;
for (int i = 0; i < n; ++i) cin >> A[i], b[i] = a[i];
Sort (b, B + N);
for (int i = 0; i < n; ++i) Dp[0][i] = ABS (A[0]-b[i]);
int ans1 = INF;
for (int i = 1; i < n; ++i) {int pre = dp[i-1][0];
for (int j = 0; J < N; ++j) {pre = min (pre, dp[i-1][j]);
DP[I][J] = pre + ABS (A[i]-b[j]);
for (int i = 0; i < n; ++i) {ans1 = min (ans1, dp[n-1][i));
}
Sort (b, B + N, CMP);
for (int i = 0; i < n; ++i) Dp[n-1][i] = ABS (A[n-1]-b[i]);
int ans2 = INF;
for (int i = n-2 i >= 0;-i) {int pre = dp[i + 1][n-1];
for (int j = n-1 J >= 0;--j) {pre = min (pre, dp[i + 1][j]);
DP[I][J] = pre + ABS (A[i]-b[j]);
for (int i = 0; i < n; ++i) {ans2 = min (ans2, dp[0][i));
} cout << min (ans1, ans2) << ' \ n ';
return 0;
}