POJ3666 Making the Grade [DP,离散化]

  题目传送门

  

Making the Grade
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9090   Accepted: 4253

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

|AB1| + |AB2| + ... + |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来做。当然蒟蒻DP本来就蒻,讲的可能不太清楚。

  根据题意,构造的只能是广义单调数列,那么就考虑单调递增的情况。

  如果要让结果尽可能小,那么肯定要求构造的序列中最大的数maxx最小,同时满足每个位置上构造的数x最小。那么设状态转移方程为dp[i][j],表示当前到了第i个位置,序列中最大的数为j,状态转移方程为dp[i][j]=abs(j-w[i])+min(d[i-1][k]) (k<=j)。当然数据的范围太大,需要离散化。但是三重循环复杂度为O(nm^2),那么每次枚举k时直接在j的循环中设置一个minn=min(min,dp[i-1][j]),把方程改为dp[i][j]=min{abs(j-w[i])+minn},可以将复杂度降低到O(nm),离散化以后就是O(n^2)。讲的肯定听不懂,那就看代码吧。

  Code:

 1 //It is made by HolseLee on 21st May 2018
 2 //POJ 3666
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<iostream>
 8 #include<iomanip>
 9 #include<algorithm>
10 #define Fi(i,a,b) for(int i=a;i<=b;i++)
11 #define Abs(a) ((a)>0?(a):-(a))
12 using namespace std;
13 typedef long long ll;
14 const int N=2018;
15 int n,m,a[N],b[N];
16 ll dp[N][N];
17 void work()
18 {
19   Fi(i,1,n){ll mn=dp[i-1][1];
20     Fi(j,1,n){mn=min(mn,dp[i-1][j]);
21       dp[i][j]=Abs(a[i]-b[j])+mn;}}
22   ll ans=dp[n][1];
23   Fi(i,1,n)ans=min(ans,dp[n][i]);
24   printf("%lld\n",ans);
25 }
26 int main()
27 {
28   ios::sync_with_stdio(false);
29   cin>>n;Fi(i,1,n){cin>>a[i];b[i]=a[i];}
30   sort(b+1,b+n+1);work();return 0;
31 }

猜你喜欢

转载自www.cnblogs.com/cytus/p/9069314.html