Making the Grade

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Problem 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
<p>* Line 1: A single integer:
<i>N</i><br>* Lines 2..<i>N</i>+1: Line
<i>i</i>+1 contains a single integer elevation:
<i>A<sub>i</sub></i> </p>
 
Output
<p>* 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.</p>
 
Sample Input
7
1
3
2
4
5
3
9
 
Sample Output
3
 
Source
PKU
 
引理一下,记|Ai-Bi|的和为S,在S最小化前提下,一定存在一种构造序列B的方案,使B中的数值都在A中出现过。
然后很容易做了……
附lyd的讲解
 

对于这个引理,简单地证明一下:

可以用数学归纳法证明。

假设b[1~k-1]都在a[]中出现过;

对于b[k],若a[k]>b[k-1],则b[k]=a[k]解最优;

      若a[k]<b[k-1],则一定存在t,使b[t~k]变为a[k]的最优解;

             或b[k]=b[k-1];

特殊地,对于1号位置,b[1]=a[1]必定为最优解。

综上,引理显然得证。

接下来进行dp

我们令dp[i][j]来表示前i个数中形成单调序列并且最后一个为j的 最小花费;那么因为最后一个数为j,所以之前的数必须小于j,所以(i,j)的花费 为min{dp[i][k]}+j-a[i];

所以状态转移方程为   dp[i][j]=min{dp[i][k]}+j-a[i];

还不知道怎么推

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 0x3f3f3f3f;
using namespace std;
int n;
int a[], num[], f[][];
int main()
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
num[i] = a[i];
}
sort(num + , num + n + );
memset(f,0x3f3f3f3f, sizeof(f));
f[][] = ;
int tmp = ;
int i, j;
for (i = ; i <= n; i++)
{
tmp = f[i-][];
for (j = ; j <= n; j++)
{
tmp = min(tmp, f[i - ][j]);
f[i][j] = abs(num[j] - a[i]) + tmp;
}
}
int ans = inf;
for (i = ; i <= n; i++)
{
ans = min(ans, f[n][i]);
}
printf("%d\n", ans);
return ;
}

POJ 3666 Making the Grade (线性dp,离散化)的更多相关文章

  1. POJ - 3666 Making the Grade(dp+离散化)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  2. poj 3666 Making the Grade(dp离散化)

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7068   Accepted: 3265 ...

  3. Poj 3666 Making the Grade (排序+dp)

    题目链接: Poj 3666 Making the Grade 题目描述: 给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? 解题思路: 对于修好的路径的每 ...

  4. poj3666/CF714E/hdu5256/BZOJ1367(???) Making the Grade[线性DP+离散化]

    给个$n<=2000$长度数列,可以把每个数改为另一个数代价是两数之差的绝对值.求把它改为单调不增or不减序列最小代价. 话说这题其实是一个结论题..找到结论应该就很好做了呢. 手玩的时候就有感 ...

  5. POJ 3666 Making the Grade (DP)

    题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了. 析:并不会做,知道是DP,但就是不会,菜....d[i][j]表示前 i 个数中,最大 ...

  6. POJ 3666 Making the Grade (DP滚动数组)

    题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...

  7. POJ 3666 Making the Grade【DP】

    读题堪忧啊,敲完了才发现理解错了..理解题必须看样例啊!! 题目链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110495#pro ...

  8. POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)

    传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total ...

  9. poj 1050 To the Max(线性dp)

    题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...

随机推荐

  1. hdu 1013 过山车 匈牙利算法(代码+详细注释)

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  2. bacula配置

    Bacula Bacula是一款开源的跨平台网络备份工具,提供基于企业级的CS的备份解决方案.可以对数据进行备份.恢复.以及完整性校验.  功能特点: 支持完全备份,增量备份,差异备份. 支持多种恢复 ...

  3. Ant里面神奇的fork

    最近两天一直在处理ant运行java程序的一个问题,用IDE直接运行类里面的main函数一切正常,但用ant跑该函数就报错误,错误的原因是运行ant任务时调用的是AntClasloader,而IDE里 ...

  4. “开始菜单”按钮今年8月将重回Windows 8

    本月早些时候微软明确表示,“开始菜单”将重新回归Windows 8操作系统.尽管微软当时并没有公布具体的时间表,但据熟悉微软内部运作的消息灵通人士透露称,“开始菜单”极有可能将出现在预计于今年8月发布 ...

  5. Magento邮件发送完美设置

    Magento新站上线伊始,不料在邮件上遇到了问题.此时常用的邮件模板已经编辑完毕,诸如New Account, New Order, Password Forget等. CentOS下发送邮件很简单 ...

  6. HPU 1437: 王小二的求值问题

    1437: 王小二的求值问题 时间限制: 1 Sec 内存限制: 128 MB提交: 141 解决: 31 统计 题目描述 题意超级简单,求一个序列的次大值. 输入 多组输入,每个测试实例占两行,包括 ...

  7. 在 Roslyn 分析语法树时添加条件编译符号的支持

    我们在代码中会写 #if DEBUG 或者 [Conditional("DEBUG")] 来使用已经定义好的条件编译符号.而定义条件编译符号可以在代码中使用 #define WAL ...

  8. 对比dfs与bfs的存储机制以及bfs与队列的关系

    dfs由于是利用递归进行遍历,所以每种情况在时空上不会出现冲突,所以可以利用数组将每种情况的各个元素的值进行存储(即存储当前位) 而bfs由于并不是利用递归,不能将每种情况的值进行不冲突地存储,但由于 ...

  9. windows下配置redis

    1.首先去GitHub上下载所需文件,这里我们下载的是zip文件 https://github.com/MicrosoftArchive/redis/releases 2.解压后文件目录如下 3.启动 ...

  10. ppt罗列项排版

    关于罗列项的排版(1,....2,......3,......4,........)