标签:
题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了。
析:并不会做,知道是DP,但就是不会,菜。。。。d[i][j]表示前 i 个数中,最大的是 j,那么转移方程为,d[i][j] = abs(j-w[i])+min(d[i-1][k]);(k<=j).
用滚动数组更加快捷,空间复杂度也低。
代码如下:
#include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> using namespace std ; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f3f; const double eps = 1e-8; const int maxn = 3e4 + 5; const int dr[] = {0, 0, -1, 1}; const int dc[] = {-1, 1, 0, 0}; int m, n; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int a[maxn], b[maxn]; LL d[maxn]; int main(){ while(scanf("%d", &n) == 1){ for(int i = 0; i < n; ++i) scanf("%d", &a[i]), b[i] = a[i]; sort(b, b+n); for(int i = 0; i < n; ++i) d[i] = abs(b[i]-a[0]); for(int i = 1; i < n; ++i){ LL mmin = d[0]; for(int j = 0; j < n; ++j){ mmin = min(mmin, d[j]); d[j] = (LL)mmin + (LL)abs(b[j]-a[i]); } } LL ans = INF; for(int i = 0; i < n; ++i) ans = min(ans, d[i]); cout << ans << endl; } return 0; }
POJ 3666 Making the Grade (DP)
标签:
原文地址:http://www.cnblogs.com/dwtfukgv/p/5754605.html