Making the Grade
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 3122 Accepted: 1476
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
思路:这题求的是不下降序列或者不上升序列。而网上的人说这道题数据水,只要求不下降的就可以了。首先定义状态dp[i][j]表示的是前 i 个数中,j 为最长不下降序列中的最大值时需要改变的花费。那么状态转移就是dp[i][j] = min(dp[i-1][1~j] + abs(a[i] - j)),意思是当前状态dp[i][j]是从上一个状态中d[i-1][1~j]中的最小值去加上abs(a[i]-j),也就是dp[i-1][1~j]中花费最少的,那么直接从这个状态转移过来,肯定也是最少的。然而 j 最大有1,000,000,000,不管是时间和空间都不允许操作。所以要离散化,既将原数列按升序排序(如果不离散化时,是从1循环到j,既从小到大的,升序排序的作用也是如此)。离散化的作用是将1,000,000,000的数变成只需要2000来表示,其中很多空余的数不需要考虑,因为它们并不存在于这个数列中。
31644k 266ms

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define SIZE 2005 6 7 using namespace std; 8 9 typedef __int64 Int; 10 11 Int dp[SIZE][SIZE]; 12 int array[SIZE]; 13 int temp[SIZE]; 14 int N; 15 16 int Abs(int a) 17 { 18 return a > 0? a:(-a); 19 } 20 21 int main() 22 { 23 while(~scanf("%d",&N)) 24 { 25 for(int i=1; i<=N; i++) 26 { 27 scanf("%d",&array[i]); 28 temp[i] = array[i]; 29 } 30 sort(temp+1,temp+1+N); 31 memset(dp,0,sizeof(dp)); 32 for(int i=1; i<=N; i++) 33 { 34 Int mini = dp[i-1][1]; 35 for(int j=1; j<=N; j++) 36 { 37 mini = min(mini,dp[i-1][j]); 38 dp[i][j] = Abs(array[i]-temp[j]) + mini; 39 } 40 } 41 Int ans = dp[N][1]; 42 for(int i=1; i<=N; i++) 43 if(ans > dp[N][i]) 44 ans = dp[N][i]; 45 printf("%I64d\n",ans); 46 } 47 return 0; 48 }
考虑到每一层状态都是从上一个状态得来的,既只与上一个状态直接相关。所以可以用滚动数组。
180k 94ms

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define SIZE 2005 6 7 using namespace std; 8 9 typedef __int64 Int; 10 11 Int dp[2][SIZE]; 12 int array[SIZE]; 13 int temp[SIZE]; 14 int N; 15 16 int Abs(int a) 17 { 18 return a > 0? a:(-a); 19 } 20 21 int main() 22 { 23 while(~scanf("%d",&N)) 24 { 25 for(int i=1; i<=N; i++) 26 { 27 scanf("%d",&array[i]); 28 temp[i] = array[i]; 29 } 30 sort(temp+1,temp+1+N); 31 memset(dp,0,sizeof(dp)); 32 for(int i=1; i<=N; i++) 33 { 34 Int mini = dp[(i+1)%2][1]; 35 for(int j=1; j<=N; j++) 36 { 37 mini = min(mini,dp[(i+1)%2][j]); 38 dp[i%2][j] = Abs(array[i]-temp[j]) + mini; 39 } 40 } 41 Int ans = dp[N%2][1]; 42 for(int i=1; i<=N; i++) 43 if(ans > dp[N%2][i]) 44 ans = dp[N%2][i]; 45 printf("%I64d\n",ans); 46 } 47 return 0; 48 }