动态规划 Making the Grade
来源:互联网 发布:北森职业测评软件 编辑:程序博客网 时间:2024/05/16 23:57
由于每个改变的都必然要变成其他的一个包含在数组内的点,所以我们利用动归dp[i][j],表示到第i个点用到第j高的数,那么递推关系就是dp[i][j]=dp[i-1]中最小的值加上a[i]和b[j]差的绝对值,最后再找dp[n][j]中的最小值就行了
#include <stdio.h>#include<algorithm>#include<math.h>using namespace std;const int maxn=2000+10;int a[maxn];int b[maxn];int dp[maxn][maxn];int n;int min(int x,int y){if(x<y)return x;elsereturn y;}int cmp(int x,int y){return x>y;}void solve(){memset(dp,0x3f,sizeof(dp));int i,j;for(i=1;i<=n;i++)dp[1][i]=abs(a[1]-b[i]);for(i=2;i<=n;i++){int mins=0x3f3f3f3f;for(j=1;j<=n;j++){mins=min(mins,dp[i-1][j]);dp[i][j]=mins+abs(a[i]-b[j]);}}}int main(){int i;scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",&a[i]);b[i]=a[i];}int ans=0x3f3f3f3f;sort(b+1,b+1+n);solve();for(i=1;i<=n;i++){ans=min(ans,dp[n][i]);}sort(b+1,b+1+n,cmp);solve();for(i=1;i<=n;i++){ans=min(ans,dp[n][i]);}printf("%d\n",ans);return 0;}
1 0
- 动态规划 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 动态规划+滚动数组
- POJ3666——Making the Grade(动态规划)
- 动态规划训练23 [Making the Grade POJ
- poj 3666 Making the Grade (动态规划)
- 动态规划——POJ3666 Making the Grade
- poj 3666 Making the Grade (离散化+动态规划)
- POJ3666 Making the Grade题解-动态规划dp
- Making the Grade(poj1527)
- POJ3666---Making the Grade
- Making the Grade
- poj3666 Making the Grade
- poj3666 Making the Grade
- POJ3666-Making the Grade
- Android安卓获取网络状态
- iOS开发 - 多线程之GCD
- Windows消息循环机制
- android UI进阶之自定义组合控件
- android studio 引入另一个XML文件的布局控件
- 动态规划 Making the Grade
- 1.3 让物体沿任意方向运动【三角函数、正弦、余弦、弧度】
- JS对象以及创建对象的几种方式
- Pku oj 3468 A Simple Problem with Integers(线段树Lazy标记)
- 使用autoconf生成Makefile并编译工程
- 【测试】简述几个测试工具的特点和功能
- 整理:Python程序员常用的IDE和其它开发工具
- Windows安装和使用zookeeper(入门)
- Curl 命令详解