poj-3666
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的更多相关文章
- 把一个序列转换成非严格递增序列的最小花费 POJ 3666
//把一个序列转换成非严格递增序列的最小花费 POJ 3666 //dp[i][j]:把第i个数转成第j小的数,最小花费 #include <iostream> #include < ...
- Poj 3666 Making the Grade (排序+dp)
题目链接: Poj 3666 Making the Grade 题目描述: 给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? 解题思路: 对于修好的路径的每 ...
- S - Making the Grade POJ - 3666 结论 将严格递减转化成非严格的
S - Making the Grade POJ - 3666 这个题目要求把一个给定的序列变成递增或者递减序列的最小代价. 这个是一个dp,对于这个dp的定义我觉得不是很好想,如果第一次碰到的话. ...
- 「POJ 3666」Making the Grade 题解(两种做法)
0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...
- POJ - 3666 Making the Grade(dp+离散化)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- POJ 3666 Making the Grade(二维DP)
题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...
- POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)
传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total ...
- poj 3666 河南省第七届程序设计D题(山区修路)
题目大意: 给定一个序列,以最小代价将其变成单调不增或单调不减序列,求最小的变动价值:需要用到离散化dp 状态转移方程: dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k ...
- 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 ...
- POJ 3666 Making the Grade
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
随机推荐
- DevExpress的GridControl如何实现打印和打印预览 z
第一种方法: System.Drawing.Printing.PageSettings set_print_page = new System.Drawing.Printing ...
- 百度 BAE 项目部署
转载:http://www.cnblogs.com/shamoyuu/p/node_bae.html 百度有一个应用引擎,价格非常便宜,Java的tomcat每天4毛钱,node每天2毛钱,我以前在上 ...
- ZOJ 3810 A Volcanic Island (2014年牡丹江赛区网络赛B题)
1.题目描写叙述:点击打开链接 2.解题思路:本题是四色定理的模板题.只是有几种情况要提前特判一下:n==1直接输出,1<n<5时候无解,n==6时候套用模板会出现同样的块.因此要特判一下 ...
- who命令
who1.c #include <stdio.h>#include <utmp.h>#include <fcntl.h>#include <unistd.h& ...
- 使用HtmlUnit登录百度
环境 java 1.7 HtmlUnit 2.18 eclipse 4.4.2 maven 配置: <dependency> <groupId>net.sourceforge. ...
- WeX5开发指南
WeX5入门.UI2开发.App开发.服务端开发.扩展资料学习. 1 新手入门 1.1 运行WeX5的demo(视频) 1.2 App开发.调试.打包部署完整过程(视频) 1.3 创建第一个应用(视频 ...
- iOS应用数据存储的经常使用方式
ios程序中数据数据存储有下列5种方式 XML属性列表(plist)归档 Preference(偏好设置) NSKeyedArchiver归档(NSCoding) SQLite3 Core Data ...
- ListView的异步载入(笔记,多线程和AsyncTask)
异步载入最经常使用的两种方式: 多线程,线程池 AsyncTask 实例操作: 从一个站点上获取Json数据.然后将数据在ListView上显示. 1.创建item_layout布局 , 改动 ...
- 按照HashMap中value值进行排序
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; ...
- MySQL安装过程中出现“APPLY security settings错误”的解决方式
***********************************************声明*************************************************** ...