描述


http://poj.org/problem?id=3666

给一串坡的高度,现在要调整某些点,使整个坡单调不降或单调不升.调整的花费为原高度与先高度的差的绝对值,问最小花费(可单增可单降).

Making the Grade
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5802   Accepted: 2717

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

3

Source

分析


分单调不增和单调不降两种情况,是一样的,我们分析单调不降的情况.

用dp[i][j]表示前i个点有序且以j结尾的最小花费.则有转移方程:

dp[i][j]=min(dp[i-1][k])+abs(a[i]-j) (0<=k<=j).

但是看数据范围发现高度可以取到10^9,而且分析可知,一个点如果需要调整,为了花费最小,只需要和左边一样就好,所以调整之后的取值一定在a数组中,显然要离散一下.

用dp[i][j]表示前i个点有序且以b[i]结尾的最小花费.则有转移方程:

dp[i][j]=min(dp[i-1][k])+abs(a[i]-a[j])(a[k]<=a[j]).

这样的话就需要三层i,j,k的循环,会超时,考虑把a数组copy一份到b,然后把b升序排列一下,这样在第二层循环里统计k<=j即b[k]<=b[j]的最小的dp[i-1][k].另外,由于只用到了i和i-1,所以可以考虑使用滚动数组.

ps.

1.POJ上数据有问题,单调不降一遍就能过,实际上应该dp两遍.

2.感觉自己好弱啊,动规基本都是看题解才做出来的= =,我这样强行作死真的大丈夫?

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define for1(i,a,n) for(int i=(a);i<=(n);i++)
#define read(a) a=getnum()
#define CC(i,a) memset(i,a,sizeof(i))
using namespace std; const int maxn=+,INF=0x7fffffff;
int n;
int a[maxn],b[maxn];
int dp[][maxn]; inline int getnum()
{
int r=,k=; char c;
for(c=getchar();c<''||c>'';c=getchar()) if(c=='-') k=-;
for(;c>=''&&c<='';c=getchar()) r=r*+c-'';
return r*k;
} bool comp(int a,int b) { return a>b; } void solve()
{
sort(b+,b+n+);
for1(i,,n) dp[][i]=abs(a[]-b[i]);
for1(i,,n)
{
int min_c=dp[(i-)&][];
for1(j,,n)
{
min_c=min(min_c,dp[(i-)&][j]);
dp[i&][j]=min_c+abs(a[i]-b[j]);
}
}
int ans=INF;
for1(i,,n) ans=min(ans,dp[n&][i]);
sort(b+,b+n+,comp);
for1(i,,n) dp[][i]=abs(a[]-b[i]);
for1(i,,n)
{
int min_c=dp[(i-)&][];
for1(j,,n)
{
min_c=min(min_c,dp[(i-)&][j]);
dp[i&][j]=min_c+abs(a[i]-b[j]);
}
}
for1(i,,n) ans=min(ans,dp[n&][i]);
printf("%d\n",ans);
} void init()
{
read(n);
for1(i,,n)
{
read(a[i]);
b[i]=a[i];
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("making.in","r",stdin);
freopen("making.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("making.out");
#endif
return ;
}

POJ_3666_Making_the_Grade_(动态规划)的更多相关文章

  1. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  2. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  3. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  4. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  5. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  6. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. DDX_Text (MFC)

    DDX_Text (MFC) 描述:该DDX_Text功能管理int的转移,UINT,long,DWORD,CString,float, 或 double编辑控件之间的数据在对话框中,表单视图或控制视 ...

  2. 安装完oracle重新启动后报ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务(重启前正常)

    安装完oracle重新启动后报ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务(重启前正常) 刚安装完后用plSql登录正常. 在dos命令行下 输入  sqlplus 用户 ...

  3. OC加强-day05

    #program mark - 0_11 NSRange结构体介绍 [掌握] 是Foundation框架中的一个结构体 NSRange 定义的一个变量的两个属性 location:起始下标 lengt ...

  4. mongodb下载及安装配置教程【仅供参考】

    1 下载 下载页面地址:https://www.mongodb.org/downloads 版本选择:电脑系统是64位的,所以我选择了 Windows 64-bit 2008 R2+ ,msi包 2 ...

  5. 命令行下上传文件到iOS软件 专业文件管理/gplayer

    U盘丢了, 就拿手机当U盘用用先. 一般情况下软件打开上传功能, 在浏览器里上传即可. 可是偏偏我的电影放在了 树莓派里面(搭建了一个SMB), 直接浏览器的话,会多占用些带宽, 我的破路由器.... ...

  6. Android清空画布

    public void clear() { Paint paint = new Paint(); paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR ...

  7. IOS 学习笔记 2015-04-15 手势密码(原)

    // // WPSignPasswordView.h // 网投网 // // Created by wangtouwang on 15/4/9. // Copyright (c) 2015年 wan ...

  8. You have new mail in /var/spool/mail/root 烦不烦你(转)

    转自(http://blog.csdn.net/yx_l128125/article/details/7425182) 有时在进入系统的时候经常提示You have new mail in /var/ ...

  9. 01_C语言基础

    内容提要: 1. C语言概述2. 数据类型.运算符与表达式3. C语言程序结构 4. VC6.0使用练习 知识详解01:C语言的历史 1. C语言与其它语言比较 汇编语言: (1).可直接对硬件进行操 ...

  10. c# 实现文件批量压缩

    今天改一个网站的功能,网站提供一些微信的素材,每个页面对应一套素材,如果会员一张一张下载,那么网站交互性就有点太差了.所以修改的内容就是提供一个按钮,点击按钮将这套图片和网站信息进行打包下载. 思路: ...