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

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

给定一个序列,以最小代价将其变成单调不增或单调不减序列,这里的代价看题目公式。

思路:

很容易想到是DP。

1.

对前i个序列,构成的最优解其实就是与两个参数有关。一个是这个序列处理后的最大值mx,和这个序列处理的代价值cost。

显然最大值mx最小最好(这样第i+1个值可以不花代价直接接在其后面的可能性更大),cost最小也最好(题意要求),但是两者往往是鱼和熊掌。

用dp[i][j]表示:前i个数构成的序列,这个序列最大值为j,dp[i][j]的值代表相应的cost。

所以状态转移方程如下:

dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k<=j)

这个表格是根据转移方程写出来的dp数组。

再仔细看一下转移方程:dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k<=j)

右边没填充的是因为填充的数字肯定比前面的数字大,无用,因为在求min( dp[i-1][k] )时,是求最小值,既然更大,则最小值时无需考虑。

又从表格中可以看出:

dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k<=j)这里的k无需从1遍历到j。

只要在对j进行for循环的时候不断更新一个dp[i-1][j]的最小值mn=min(mn,dp[i-1][j]),

然后对dp[i][j]=abs(j-w[i])+mn即可;

这样改进之后即可从本来的时候时间复杂度O(NMM)改进为O(NM);

但是,这里的m是A[i]的最大值,显然TLE。

所以必须用离散化思想改进,因为N=2000。远小于A[i]的最大值。

离散化:将序列排序一下,然后用位置的前后关系来制定其值,这样时间复杂度变成O(N^2).

最后是这题数据有bug,只需要求不减序列即可。

?:谁能给我讲讲为什么这样离散化?为什么最后的结果中的每一个的数字都在原来的串中出现过?为什么这样得到的就是一个最优解?

附个解题报告,没太看懂他的证明

http://www.cnblogs.com/vb4896/p/5877962.html

 #include<iostream>
#include <cstdio>
#include<vector>
#include<algorithm>
#define Abs(a) ((a)>0?(a):-(a))
#define Mod(a,b) (((a)-1+(b))%(b)+1)
using namespace std;
const int N=;
const long long inf=(<<);
int n;
int a[N],b[N];
long long int dp[N][N];
void solve()
{
for(int i=;i<=n;i++)
{
long long mn=dp[i-][];
for(int j=;j<=n;j++)
{
mn=min(mn,dp[i-][j]);
dp[i][j]=Abs(a[i]-b[j])+mn;
}
}
long long ans=dp[n][];
for(int i=;i<=n;i++)
ans=min(ans,dp[n][i]);
printf("%lld\n",ans);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",a+i);
b[i]=a[i];
}
sort(b+,b+n+);
solve(); return ;
}
题目给出了一列数,要求通过修改某些值,使得最终这列数变成有序的序列,非增或者非减的(数据很弱,只用求非严格递增即可),求最小的修改量。
首先我们会发现,最终修改后,或者和前一个数字一样,或者和后一个数字一样,这样才能修改量最小。
我们先根据原数列排序,确定元素的大小关系,对应编号为p[i]
dp[i][j] 表示考虑前i个元素,最后元素为序列中 第j小元素的最优解
dp[i][j] = MIN(dp[i-1][k]) + abs(a[i]-a[p[j]]), (0<k<=j)    // 非严格递增
 //二维解法
#include<iostream>
#include <cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=; int n;
__int64 a[N];
__int64 b[N]; bool cmp(__int64 a,__int64 b)
{
return a<b;
} __int64 dp[N][N];
__int64 min(__int64 x,__int64 y)
{
return x<y?x:y;
}
int main()
{
//freopen("in.txt","r",stdin);
__int64 i,j,k,ans;
cin>>n;
for(i=; i<=n; i++) {
scanf("%I64d",&a[i]);
}
for(i=; i<=n; i++) {
b[i]=a[i];
}
sort(b+,b++n,cmp);
for(j=; j<=n; j++) {
dp[][j]=a[]-b[j];
if(dp[][j]<) {
dp[][j]=-dp[][j];
}
}
__int64 t;
for(i=; i<=n; i++) { // 非严格递增
k=dp[i-][];
for(j=; j<=n; j++) {
k=min(dp[i-][j],k);
t=a[i]-b[j];
if(t<) {
t=-t;
}
dp[i][j]=k+t; //dp[i][j] = min{dp[i - 1][0...j] + ?abs(a[i] - b[j])}
}
}
ans=dp[n][n];
for(i=n; i>=; i--) {
ans=min(ans,dp[n][i]);
}
printf("%I64d\n",ans);
return ;
}
 //一维 (滚动数组)
#include<iostream>
#include <cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=;
__int64 a[N],b[N],dp[][N];
__int64 min(__int64 x,__int64 y)
{
return x<y?x:y;
}
int main()
{
int n,i,j;
__int64 k,t;
scanf("%d",&n);
for(i=; i<=n; i++) {
scanf("%I64d",&a[i]);
}
for(i=; i<=n; i++) {
b[i]=a[i];
}
sort(b+,b++n);
for(i=; i<=n; i++) {
dp[][i]=a[]-b[i];
if(dp[][i]<) {
dp[][i]=-dp[][i];
}
}
for(i=; i<=n; i++) {
k=dp[i%][];
for(j=; j<=n; j++) {
k=min(k,dp[i%][j]);
t=a[i]-b[j];
if(t<) {
t=-t;
}
dp[(i+)%][j]=k+t;
}
}
j=(n+)%;
k=dp[j][];
for(i=; i<=n; i++) {
k=min(k,dp[j][i]);
}
printf("%I64d\n",k);
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 eleva ...

  2. poj 3666 Making the Grade(dp)

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

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

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

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

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

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

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

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

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

  7. POJ 3666 Making the Grade (线性dp,离散化)

    Making the Grade Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) T ...

  8. POJ 3666 Making the Grade(二维DP)

    题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...

  9. POJ 3666 Making the Grade (DP)

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

随机推荐

  1. 【转】【FTP】之windows8.1上搭建FTP服务器方法

    参考地址:<windows8.1上搭建FTP服务器方法>

  2. Ajax主要用来完成哪些工作?

    AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下. 它使用 JavaScript 在 web 浏览器与 web 服务器之间来发送和接收数据. ajax主要用于在页面内容加 ...

  3. 大数据hadoop之zookeeper

    一.ZooKeeper 的实现 1.1 ZooKeeper处理单点故障 我们知道可以通过ZooKeeper对分布式系统进行Master选举,来解决分布式系统的单点故障,如图所示. 图 1.1 ZooK ...

  4. 3s 简介

    "3S"技术是英文遥感技术(Remote Sensing RS).地理信息系统(Geographical information System GIS).全球定位系统(Global ...

  5. ASP.NET动态网站制作(19)-- C#(2)

    前言:C#的第二次课,依旧讲解C#的基础知识. 内容: 1.GC:垃圾回收机制,可以回收托管模块中的垃圾. 2.值类型和引用类型:  (1)值类型:所有的数值类型都是值类型,如int,byte,sho ...

  6. 在VS中自动生成NuGet包以及搭建自己的或单位内部的NuGet服务器

    关于NuGet的介绍已经很多,可以参考下面的: NuGet学习笔记(1)--初识NuGet及快速安装使用 http://kb.cnblogs.com/page/143190/ NuGet学习笔记(2) ...

  7. c3p0;maven;model;

  8. Office 365系列(-)

    昨天参加上海微软TechED技术大会,看见很多传说中的大牛,听了涂曙光老师等人的讲座,激情澎湃啊,看见他们对技术以及程序员社区的投入及激情,十分敬佩.自己搞IT行业也已经10多年了,平常都很少写博客和 ...

  9. zookeeper snowflake 实战

    目录 写在前面 1.1.1. 集群节点的命名服务 1.1.2. snowflake 的ID算法改造 SnowFlake算法的优点: SnowFlake算法的缺点: 写在最后 疯狂创客圈 亿级流量 高并 ...

  10. Python菜鸟之路:Python基础-模块

    什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,分组的规则就是把实现了某个 ...