[poj 3666] Making the Grade (离散化 线性dp)
2018-03-14 22:48
387 查看
今天的第一题(/ω\)!
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
4000
3
Source
USACO 2008 February Gold
code:
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
4000
3
Source
USACO 2008 February Gold
code:
//By Menteur_Hxy #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> using namespace std; const int MAX=2010; const int INF=0x3f3f3f3f; int n,a[MAX],num[MAX],f[MAX][MAX]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); num[i]=a[i]; } sort(num+1,num+n+1); int m=unique(num+1,num+n+1)-num-1; memset(f,0x3f,sizeof f); f[0][0]=0; for(int i=1;i<=n;i++) { int temp=f[i-1][0]; for(int j=1;j<=m;j++) { temp=min(temp,f[i-1][j]); f[i][j]=temp+abs(a[i]-num[j]); } } int ans=INF; for(int i=1;i<=m;i++) ans=min(ans,f [i]); printf("%d",ans); return 0; }
相关文章推荐
- 【POJ 3666】Making the Grade(离散化+DP)
- POJ 3666 Making the Grade dp + 离散化
- POJ 3666 Making the Grade 【DP+离散化】
- poj 3666 Making the Grade(dp离散化)
- POJ 3666 Making the Grade——DP + 离散化
- [POJ 3666] Making the Grade (序列DP+离散化)
- POJ3666 Making the Grade [DP,离散化]
- poj 3666 Making the Grade(dp离散化)
- poj 3666 Making the Grade (有序序列,离散化dp)
- Making the Grade poj 3666(离散化+滚动数组+dp)
- poj 3666 Making the Grade dp 离散化
- POJ 3666-Making the Grade (DP+离散化)
- 【POJ 3666】Making the Grade(简单DP)
- 【dp】POJ - 3666 Making the Grade
- POJ 3666 Making the Grade DP
- POJ 3666 Making the Grade 【DP + 离散化 + 贪心 】
- DP:Making the Grade(POJ 3666)
- POJ 3666 Making the Grade (DP)
- POJ - 3666 Making the Grade(DP)
- POJ 3666 Making the Grade 滚动数组+状态压缩DP