poj 3666 Making the Grade(dp离散化)
来源:互联网 发布:js 判断是否json对象 编辑:程序博客网 时间:2024/05/30 04:55
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
思路:naive的DP解法如下:
#include <cstdio>#include <cstdlib>#include <string.h>#include <string>#include <math.h>#include <algorithm>#include <iostream>#include <queue>#include <stack>#include <map>#include <set>using namespace std;int i,j,t,n,m=0;int a[2010],b[2010];int dp[2010][1000000010];const int inf = 1<<30;int main(){ cin>>n; for(i=0;i<n;i++) { cin>>a[i]; b[i]=a[i]; m=max(m,a[i]); } sort(b,b+n); for(i=0;i<=m;i++) dp[0][i]=abs(a[i]-i); // 先写naive的,再优化 for(i=1;i<n;i++) for(j=0;j<=m;j++) { int tmp = inf; for(k=0;k<j;k++) tmp=max(tmp,dp[i-1][k]); dp[i][j]=tmp+abs(a[i]-j) } int ret=inf; for(i=0;i<=m;i++) ret=min(ret,dp[n-1][i]); cout<<ret<<endl; return 0;}
但是开的空间太大,需要离散化,因为取值就在a数组中的那些,另外开一个数组用index标识就好啦
#include <cstdio>#include <cstdlib>#include <string.h>#include <string>#include <math.h>#include <algorithm>#include <iostream>#include <queue>#include <stack>#include <map>#include <set>using namespace std;int i,j,k,t,n;int a[2010],b[2010];int dp[2010][2010];const int inf = 1<<30;int main(){ cin>>n; for(i=0;i<n;i++) { cin>>a[i]; b[i]=a[i]; } sort(b,b+n); for(i=0;i<n;i++) dp[0][i]=abs(a[0]-b[i]);// for(i=0;i<n;i++)// cout<<dp[0][i]<<" ";// cout<<endl; // 先写naive的,再优化 for(i=1;i<n;i++) { for(j=0;j<n;j++) { int tmp = inf; for(k=0;k<=j;k++) tmp=min(tmp,dp[i-1][k]); dp[i][j]=tmp+abs(a[i]-b[j]); }// for(j=0;j<n;j++)// cout<<dp[i][j]<<" ";// cout<<endl; } int ret=inf; for(i=0;i<n;i++) ret=min(ret,dp[n-1][i]); cout<<ret<<endl; return 0;}
然后DP过程有重复枚举过程,优化下
#include <cstdio>#include <cstdlib>#include <string.h>#include <string>#include <math.h>#include <algorithm>#include <iostream>#include <queue>#include <stack>#include <map>#include <set>using namespace std;int i,j,t,n,m=0;int a[2010],b[2010];int dp[2010][2010];const int inf = 1<<30;int main(){ cin>>n; for(i=0;i<n;i++) { cin>>a[i]; b[i]=a[i]; m=max(m,a[i]); } sort(b,b+n); for(i=0;i<=m;i++) dp[0][i]=abs(a[i]-i); // 先写naive的,再优化 for(i=1;i<n;i++) for(j=0;j<=m;j++) { int tmp = inf; for(k=0;k<=j;k++) tmp=min(tmp,dp[i-1][k]); dp[i][j]=tmp+abs(a[i]-j); } int ret=inf; for(i=0;i<=m;i++) ret=min(ret,dp[n-1][i]); cout<<ret<<endl; 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+离散化)
- 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 + 离散化
- 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)
- POJ3666 Making the Grade [DP,离散化]
- 新手上路,坚持,多努力~
- sklearn中的朴素贝叶斯算法
- java8 Stream API各种流操作应用(2)
- java IO流学习总结
- Ubuntu安装Cassandra-3.11.1
- poj 3666 Making the Grade(dp离散化)
- python基础学习
- SSM框架原理作用及使用方法
- Linux内核移植(6)
- 【1】tinkerpop / gremlin定义一个属性图
- Xlua
- 关于java中判断为空出现空指针异常的问题
- hadoop-mapreduce map端多表合并
- Python3与OpenCV3.3 图像处理(十八)--Canny边缘提取