POJ 3666 Making the Grade (动态规划)
Description
A straight dirt road connects two fields on FJ's farm, but it changes elevation more than FJ would like. His cows do not mind climbing up or down a single slope, but they are not fond of an alternating succession of hills and valleys. FJ would like to add and remove dirt from the road so that it becomes one monotonic slope (either sloping up or down).
You are given N integers A1, ... , AN (1 ≤ N ≤ 2,000) describing the elevation (0 ≤ Ai ≤ 1,000,000,000) at each of N equally-spaced positions along the road, starting at the first field and ending at the other. FJ would like to adjust these elevations to a new sequence B1, . ... , BN that is either nonincreasing or nondecreasing. Since it costs the same amount of money to add or remove dirt at any position along the road, the total cost of modifying the road is
|A1 - B1| + |A2 - B2| + ... + |AN - BN |
Please compute the minimum cost of grading his road so it becomes a continuous slope. FJ happily informs you that signed 32-bit integers can certainly be used to compute the answer.
Input
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single integer elevation: Ai
Output
* Line 1: A single integer that is the minimum cost for FJ to grade his dirt road so it becomes nonincreasing or nondecreasing in elevation.
Sample Input
7 1 3 2 4 5 3 9
Sample Output
3
Source
#include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #define fuck(x) cout<<#x<<" = "<<x<<endl; #define debug(x,i,j) cout<<#x<<"["<<i<<"]["<<j<<"] = "<<x[i][j]<<endl; #define ls (t<<1) #define rs ((t<<1)+1) using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 100086; const int inf = 2.1e9; const ll Inf = 999999999999999999; const int mod = 1000000007; const double eps = 1e-6; const double pi = acos(-1); int num[2008],p[2008]; int n; int dp[2008][2008]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&num[i]); p[i]=num[i]; } sort(p+1,p+1+n); int m=unique(p+1,p+1+n)-p-1; for(int i=1;i<=n;i++){ int t=lower_bound(p+1,p+1+n,num[i])-p-1; for(int j=1;j<=m;j++){ dp[i][j]=inf; for(int k=1;k<=j;k++){ dp[i][j]=min(dp[i-1][k]+abs(num[i]-p[k]),dp[i][j]); } } } printf("%d\n",dp[n][m]); return 0; }
#include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #define fuck(x) cout<<#x<<" = "<<x<<endl; #define debug(x,i,j) cout<<#x<<"["<<i<<"]["<<j<<"] = "<<x[i][j]<<endl; #define ls (t<<1) #define rs ((t<<1)+1) using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 100086; const int inf = 2.1e9; const ll Inf = 999999999999999999; const int mod = 1000000007; const double eps = 1e-6; const double pi = acos(-1); int num[2008],p[2008]; int n; int dp[2008][2008]; int minn[2008]; int main() { // ios::sync_with_stdio(false); // freopen("in.txt","r",stdin); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&num[i]); p[i]=num[i]; } sort(p+1,p+1+n); int m=unique(p+1,p+1+n)-p-1; for(int i=1;i<=n;i++){ int t=lower_bound(p+1,p+1+n,num[i])-p-1; minn[0]=inf; for(int j=1;j<=m;j++){ minn[j]=min(minn[j-1],dp[i-1][j]+abs(num[i]-p[j])); } for(int j=1;j<=m;j++){ dp[i][j]=minn[j]; } } printf("%d\n",dp[n][m]); return 0; }
如有侵权,联系删除
2290713181@qq.com