EOJ:Making the Grade
Making the Grade
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submits: 299 | Accepted: 47 |
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 A_1, . . . , A_N (1 <= N <= 2,000) describing the elevation (0 <= A_i <= 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 B_1, . . . , B_N 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
|A_1 - B_1| + |A_2 - B_2| + ... + |A_N - B_N|
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.
You are given N integers A_1, . . . , A_N (1 <= N <= 2,000) describing the elevation (0 <= A_i <= 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 B_1, . . . , B_N 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
|A_1 - B_1| + |A_2 - B_2| + ... + |A_N - B_N|
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: A_i
* Lines 2..N+1: Line i+1 contains a single integer elevation: A_i
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
71324539
Sample Output
3
Hint
By changing the first 3 to 2 and the second 3 to 5 for a total cost of |2-3|+|5-3| = 3 we get the nondecreasing sequence 1,2,2,4,5,5,9.
_________________________________________________________________________________________________
题解:
最后修改完的数列中的数必然是原数列中的数。假设元素列中有一些数已经满足条件,那么对与剩下的数,要么把他变成和左边的满足条件的数一样,要么变成和右边的一样
DP
dp[i][j]表示前I个数变成不上升序列且第I个数变成原序列中第J大的数时的最小花费。
Dp[i][j]=min{dp[i-1][k]+abs(a[j]-s[k])}(1<=k<=n) 这样就是O(N^3)的算法,仍然会超时
设辅助数组TEMP[I][J]=MIN(DP[I-1][K])
所以dp[i][j]=temp[i-1][j]+abs(a[i]-s[j]); (s数组是将原数组排序后的数组,s[j]表示原数组中第J大的数)
Temp[i][j]=min{temp[i][j-1],dp[i][j]}
这样时间复杂度降为O(N^2)
代码:
代码
1 #include<math.h>
2 #include<stdio.h>
3 long long a[2005],b[2005],d[2005],dp[2005][2005],temp[2005][2005];
4 int i,j,n;
5 long long temp1,temp2,ans;
6 long long min(long long a,long long b)
7 {
8 if (a>b) return b;
9 return a;
10 }
11 void swap(int i,int j)
12 {
13 int temp;
14 temp=b[i];
15 b[i]=b[j];
16 b[j]=temp;
17
18 }
19 void qsort(int l,int r)
20 {
21 int i,j;
22 long long x;
23 x=b[r];
24 i=l-1;
25 for (j=l;j<r;j++)
26 if (b[j]<x)
27 {
28 i++;
29 swap(i,j);
30 }
31 swap(i+1,r);
32 if (i+2<r) qsort(i+2,r);
33 if (i>l) qsort(l,i);
34 }
35 void work()
36 {
37 for (j=1;j<=n;j++)
38 {
39 dp[0][j]=0;
40 temp[0][j]=0;
41 }
42 for (i=1;i<=n;i++)
43 {
44 temp[i][0]=100000000000;
45 for (j=1;j<=n;j++)
46 {
47 dp[i][j]=temp[i-1][j]+abs(a[i]-b[j]);
48 temp[i][j]=min(temp[i][j-1],dp[i][j]);
49 }
50 }
51 }
52 int main()
53 {
54 scanf("%d",&n);
55 for (i=1;i<=n;i++)
56 {
57 scanf("%lld",&d[i]);
58 a[i]=d[i];
59 b[i]=a[i];
60 }
61 qsort(1,n);
62 work();
63 ans=100000000000;
64 ans=min(ans,temp[n][n]);
65 for (i=1;i<=n;i++)
66 a[i]=d[n-i+1];
67 work();
68 ans=min(ans,temp[n][n]);
69 printf("%lld\n",ans);
70 }
71