POJ-3666 Making the Grade
来源:互联网 发布:另类宠物知乎 编辑:程序博客网 时间:2024/06/10 19:15
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
71324539
Sample Output
3
Source
难点:注意离散化(即对原序列先进行排序),再DP
相关链接:http://blog.csdn.net/wuyanyi/article/details/7255154
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<limits.h>#include<math.h>#include<algorithm>using namespace std;long long dp[2005][2005];int main(){int n,a[2005],i,j;long long mins;int b[2005];while(scanf("%d",&n)!=EOF){mins=INT_MAX;memset(dp,0,sizeof(dp));for(i=1;i<=n;i++){scanf("%d",&a[i]);b[i]=a[i];}sort(b+1,b+n+1);for(i=1;i<=n;i++){mins=dp[i-1][1];for(j=1;j<=n;j++){mins=min(mins,dp[i-1][j]);dp[i][j]=abs(a[i]-b[j])+mins;}}mins=dp[n][1];for(i=1;i<=n;i++)mins=min(dp[n][i],mins);printf("%lld\n",mins);}}
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade
- poj 3666--Making the Grade
- POJ 3666 Making the Grade
- poj 3666 Making the Grade
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade
- POJ-3666 Making the Grade
- POJ-3666 Making the Grade
- poj 3666 Making the Grade
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade [DP]
- POJ 3666 Making the Grade(左偏树)
- POJ 3666 Making the Grade (dp)
- poj 3666 Making the Grade (动态规划)
- 【POJ 3666】Making the Grade(简单DP)
- 大话图像处理之入门篇
- 31. Next Permutation
- ROS学习之 cpp节点句柄
- maven install Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-wa
- UIautomator输入中文
- POJ-3666 Making the Grade
- 转json工具类
- 455 - Periodic Strings
- 新环境部署(linux硬盘挂载项目迁移启动)
- 01背包问题
- Java在线反编译工具
- SURF算法
- ThreadPool四种线程池
- 227 - Puzzle