http://vjudge.net/problem/POJ-3666

题目是dp 题目;   简单dp 离散一下就好.

我们先来讲一讲不离散的,简单的懂了,其他的也很容易. dp[i] 代表这个数列以i 结尾的最小花费;  假设现在要求 前n个数组成的数列,那么dp[i]= 前 n-1 的 min(dp[i]~dp[0])+ (当前这个数-i);

#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h> using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const int MAXSIZE=1e6+5;
const double eps=0.0000000001;
void fre()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
}
#define memst(a,b) memset(a,b,sizeof(a))
#define fr(i,a,n) for(int i=a;i<n;i++) const int MAXN=2010;
int a[MAXN],index[MAXN];
int dp[MAXN],temp[MAXN];
bool cmp(int x,int y)
{
return a[x]<a[y];
}
int solve(int n)
{
for(int i=0;i<n;i++) temp[i]=abs(a[0]-a[index[i]]);
for(int k=0;k<n-1;k++)
{
int minnum=INF;
for(int i=0;i<n;i++)
{
minnum=min(minnum,temp[i]);
dp[i]=abs(a[k+1]-a[index[i]])+minnum;
}
for(int i=0;i<n;i++) temp[i]=dp[i];
}
int res=INF;
for(int i=0;i<n;i++)
res=min(res,dp[i]);
return res;
}
int main(int argc,char *argv[])
{
int n;
while(scanf("%d",&n)+1)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]),index[i]=i;
sort(index,index+n,cmp);
printf("%d\n",solve(n));
}
return 0;
} /**************************************************/
/** Copyright Notice **/
/** writer: wurong **/
/** school: nyist **/
/** blog : http://blog.csdn.net/wr_technology **/
/**************************************************/

poj-3666的更多相关文章

  1. 把一个序列转换成非严格递增序列的最小花费 POJ 3666

    //把一个序列转换成非严格递增序列的最小花费 POJ 3666 //dp[i][j]:把第i个数转成第j小的数,最小花费 #include <iostream> #include < ...

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

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

  3. S - Making the Grade POJ - 3666 结论 将严格递减转化成非严格的

    S - Making the Grade POJ - 3666 这个题目要求把一个给定的序列变成递增或者递减序列的最小代价. 这个是一个dp,对于这个dp的定义我觉得不是很好想,如果第一次碰到的话. ...

  4. 「POJ 3666」Making the Grade 题解(两种做法)

    0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...

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

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

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

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

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

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

  8. poj 3666 河南省第七届程序设计D题(山区修路)

    题目大意: 给定一个序列,以最小代价将其变成单调不增或单调不减序列,求最小的变动价值:需要用到离散化dp 状态转移方程: dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k ...

  9. 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 ...

  10. 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 toolbar 假标题居中

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar_top" android:layout_height= ...

  2. linux 中两个文档怎么对比内容是否一致

    可以用diff命令对比文档内容.[语法]: diff [参数] 文件1 文件2[说明]: 本命令比较两个文本文件,将不同的行列出来-b 将一串空格或TAB 转换成一个空格或TAB-e 生成一个编辑角本 ...

  3. 转:Hadoop和Spark的异同

    转自:http://www.techweb.com.cn/network/system/2016-01-25/2267414.shtml 谈到大数据,相信大家对Hadoop和Apache Spark这 ...

  4. Spring延迟加载

    如下内容引用自:http://www.cnblogs.com/wcyBlog/p/3756624.html 1.Spring中lazy-init详解ApplicationContext实现的默认行为就 ...

  5. 读陈浩的《C语言结构体里的成员数组和指针》总结,零长度数组

    原文链接:C语言结构体里的成员数组和指针 复制例如以下: 单看这文章的标题,你可能会认为好像没什么意思.你先别下这个结论,相信这篇文章会对你理解C语言有帮助.这篇文章产生的背景是在微博上,看到@Lar ...

  6. 【转载】ASP和ASP.NET根本区别

    ASP.NET和ASP的最大区别在于编程思维的转换,而不仅仅在于功能的增强.ASP使用VBS/JS这样的脚本语言混合html来编程,而那些脚本语言属于弱类型.面向结构的编程语言,而非面向对象,这就明显 ...

  7. python发送post请求上传文件,无法解析上传的文件

    前言 近日,在做接口测试时遇到一个奇葩的问题. 使用post请求直接通过接口上传文件,无法识别文件. 遇到的问题 以下是抓包得到的信息: 以上请求是通过Postman直接发送请求的. 在这里可以看到消 ...

  8. 《UNIX 网络编程 第二版》编译环境的搭建( 运行本专栏代码必读 )

    第一步:搭建基本的编译环境 安装gcc, g++, bulid-essential等编译软件 第二步:下载本书示例源码包 可在这里下载http://ishare.iask.sina.com.cn/f/ ...

  9. 九度OJ 1089:数字反转 (数字反转)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3059 解决:1678 题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个 ...

  10. Package template (html/template) ... Types HTML, JS, URL, and others from content.go can carry safe content that is exempted from escaping. ... (*Template) Funcs ..

    https://godoc.org/text/template GoDoc Home About Go: text/templateIndex | Examples | Files | Directo ...