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. Go -- 接口赋值

    在go语言中,接口赋值分为2中情况: 1.将对象实例赋值给接口: 2.将一个接口赋值给另一个接口. 1.将对象实例赋值给接口: 要求对象实现了接口的所有方法. 2.将接口赋值给另一个接口: 假设接口A ...

  2. mac mysql忘记密码解决办法

    http://www.jb51.net/article/87580.htm http://blog.csdn.net/soft2buy/article/details/50223373

  3. 手动安装pip

    apt-get instal pip  成功之后,有根据pip的提示,进行了升级,升级之后,pip就出问题了 为了解决上面问题,手动安装pip,依次执行下面命令 1 2 3 4 5 [root@min ...

  4. 转:在CentOS下编译安装GCC

    转:https://teddysun.com/432.html 在CentOS下编译安装GCC 技术  秋水逸冰  发布于: 2015-09-02  更新于: 2015-09-02  6519 次围观 ...

  5. myBatis-plus异常提示For input string: "{0=null}"

    异常信息 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Per ...

  6. python入门之搭建环境

    进入以下网站:python.org 选择你喜欢(需要)的版本下载 点击下载即可,本次提供下载:python3.6.3 (国外架设,非常慢) ,用百度的平台吧:python3.6.1,多谢百度. 开始安 ...

  7. [Maven实战](9)传递性依赖

    了解Spring的朋友都知道.创建一个Spring Framework项目都须要依赖什么样的Jar包.假设不使用Maven,那么在项目中就须要手动下载相关的依赖.因为Spring Framework又 ...

  8. FFmpeg源码简单分析:结构体成员管理系统-AVOption

    ===================================================== FFmpeg的库函数源码分析文章列表: [架构图] FFmpeg源码结构图 - 解码 FFm ...

  9. java中的File文件读写操作

    之前有好几次碰到文件操作方面的问题,大都由于时间太赶而没有好好花时间去细致的研究研究.每次都是在百度或者博客或者论坛里面參照着大牛们写的步骤照搬过来,之后再次碰到又忘记了.刚好今天比較清闲.于是就在网 ...

  10. 兼容最新firefox、chrome和IE的javascript图片预览实现代码

    这篇文章主要介绍了兼容最新firefox.chrome和IE的javascript图片预览实现代码,测试了浏览器firefox6.firefox12.chrome 25.0.1364.172 m.IE ...