题目大意
给出了一列数,要求通过修改某些值,使得最终这列数变成有序的序列,非增或者非减的,求最小的修改量。
分析
首先我们会发现,最终修改后,或者和前一个数字一样,或者和后一个数字一样,这样才能修改量最小。
我们先根据原数列排序,确定元素的大小关系,对应编号为p[i]
dp[i][j] 表示考虑前i个元素,最后元素为序列中 第j小元素的最优解
dp[i][j] = MIN(dp[i-1][k]) + abs(a[i]-a[p[j]]), (0<k<=j)   
刚开始以为是o(n^3)的复杂度,后来想想,k值在每次j循环的时候就能确定最小值
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define INF 100000000000000
#define maxn 2005
using namespace std;
int n;
typedef long long LL;
LL a[maxn],b[maxn];
LL dp[maxn][maxn];
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=; i<=n; i++)
{
scanf("%I64d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
dp[i][j]=INF;
for(int j=; j<=n; j++)
{
dp[][j]=(a[]-b[j]);
if(dp[][j]<)
dp[][j]=-dp[][j];
} for(int i=; i<=n; i++)
{
LL k=dp[i-][];
for(int j=; j<=n; j++)
{
k=min(dp[i-][j],k);
LL tem=(a[i]-b[j]);
if(tem<)
tem=-tem;
dp[i][j]=min(dp[i][j],k+tem);
// printf("==%I64d\n",dp[i][j]);
}
}
LL minn=INF;
for(int i=; i<=n; i++)
minn=min(minn,dp[n][i]);
printf("%I64d\n",minn);
}
return ;
}

hdu 3666 Making the Grade的更多相关文章

  1. POJ 1364 / HDU 3666 【差分约束-SPFA】

    POJ 1364 题解:最短路式子:d[v]<=d[u]+w 式子1:sum[a+b+1]−sum[a]>c      —      sum[a]<=sum[a+b+1]−c−1  ...

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

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

  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. HDU 3666.THE MATRIX PROBLEM 差分约束系统

    THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. kaungbin_DP S (POJ 3666) Making the Grade

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

  7. POJ 3666 Making the Grade

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

  8. poj 3666 Making the Grade(dp)

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

  9. POJ 3666 Making the Grade (动态规划)

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

随机推荐

  1. Android 浮动搜索框 searchable 使用(转)。

    Android为程序的搜索功能提供了统一的搜索接口,search dialog和search widget,这里介绍search dialog使用.search dialog 只能为于activity ...

  2. LCA-倍增法(在线)

    原文:http://www.tuicool.com/articles/N7jQV32 1. DFS预处理出所有节点的深度和父节点 inline void dfs(int u) { int i; for ...

  3. nuget的小Tips

    1.nuget常用命令 nuget spec -a bin\Debug\DllName.dll -f //根据dll生成.nuspec文件,这样会生成无用的默认标签,比如licenseUrl.tags ...

  4. java中的日志组件-log4j

    1.为什么使用日志组件 Log4J是Apache的一个开放源代码项目,它是一个日志操作包,通过使用Log4J,可以指定日志信息输出的目的地,如控制台.文件.CUI组件.NT的事件记录器:还可以控制每一 ...

  5. CSU1022

    题目: blue和AutoGerk是好朋友.他们的相同点是都喜欢研究算法,不同点是AutoGerk已是大牛而blue还是菜鸟.blue经常拿一些自以为很难的问题去问AutoGerk,想难倒他,但是每次 ...

  6. ubuntu安装多个qt版本--不同qt版本编译同一个程序时出现错误--解决方案

    方法: 在ubuntu终端: # make clean   //有Makefile文件的情况 # rm Makefile *.pro.user # qmake  //有多个qt版本,最好指定qmake ...

  7. 计算文字的高度和宽度--以微博会话界面中用户名(userName)为例

    所用方法 // NOTE: All of the following methods will default to drawing on a baseline, limiting drawing t ...

  8. postgreSQL初步使用总结

    一.安装 postgreSQL安装完成后会默认生成一个名为postgres的用户和一个名为postgres的数据库.可以使用自带的psql.exe工具来登录.其帮助信息如下 连接到本地的postgre ...

  9. CentOS中vsftp安装与配置

    http://blog.chinaunix.net/uid-7271021-id-3086186.html 553 Could not create file 解决办法 [root@localhost ...

  10. php空心菱形

    <?php for($i=1;$i<=9;$i++){ for($j=1;$j<=9-$i;$j++){ echo " "; } for($k=1;$k<= ...