poj 3666 Making the Grade(离散化+dp)
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[i][j]=abs(j-a[i])+min(dp[i-1][k]);(k<=j) i表示前i个数 最大高度为j的 最小花费 而j很大有1e10 所以显然不能直接开这么大的数组 所以 我们可以
把现有的高度存进数组 再对数组排序(离散化) 这样 j就表示为在数组里下标为j的高度。
然后 min(dp[i-1][k]) 对于这个最小值 我们也没有必要再用一次循环取求 因为 k<=j 所以每次只需要用一个变量去跟新最小值即可
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int a[];
int b[];
ll dp[][]; //dp[i][j]=abs(j-a[i])+min(dp[i-1][k]);(k<=j)
int n;
ll solveup(){
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
ll minn=dp[i-][];
for(int j=;j<=n;j++){
minn=min(minn,dp[i-][j]);
dp[i][j]=abs(b[j]-a[i])+minn;
}
}
ll ans=1e18;
for(int i=;i<=n;i++)
ans=min(ans,dp[n][i]);
return ans;
}
int main(){
ios::sync_with_stdio(false);
while(cin>>n){
for(int i=;i<=n;i++){
cin>>a[i];
b[i]=a[i];
}
sort(b+,b++n);
cout<<solveup()<<endl;
}
return ;
}
poj 3666 Making the Grade(离散化+dp)的更多相关文章
- Poj 3666 Making the Grade (排序+dp)
题目链接: Poj 3666 Making the Grade 题目描述: 给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? 解题思路: 对于修好的路径的每 ...
- 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离散化)
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7068 Accepted: 3265 ...
- POJ 3666 Making the Grade (DP)
题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了. 析:并不会做,知道是DP,但就是不会,菜....d[i][j]表示前 i 个数中,最大 ...
- POJ 3666 Making the Grade (DP滚动数组)
题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...
- POJ 3666 Making the Grade【DP】
读题堪忧啊,敲完了才发现理解错了..理解题必须看样例啊!! 题目链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110495#pro ...
- POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)
传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ 3666 Making the Grade (线性dp,离散化)
Making the Grade Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) T ...
- [poj 3666] Making the Grade (离散化 线性dp)
今天的第一题(/ω\)! Description A straight dirt road connects two fields on FJ's farm, but it changes eleva ...
随机推荐
- MySQL之慢查询日志和通用查询
MySQL中的日志包括:错误日志.二进制日志.通用查询日志.慢查询日志等等.这里主要介绍下比较常用的两个功能:通用查询日志和慢查询日志. 1.通用查询日志:记录建立的客户端连接和执行的语句. 2.慢查 ...
- css行内省略号、垂直居中
应用场景分析: 一.当你的文字限定行数,超出部分的文字用省略号显示. (有两个使用场景:1.单行 2.多行) // 单行 overflow: hidden; text-overflow:ellipsi ...
- 微信QQ打开网页时提示用浏览器打开
微信QQ打开网页时提示用浏览器打开 一,需求分析 1.1,使用微信或QQ打开网址时,无法在微信或QQ内打开常用下载软件,手机APP等.故此需要在微信qq里提示 二,功能实现 2.1 html实现 &l ...
- golang操作mysql使用总结
前言 Golang 提供了database/sql包用于对SQL数据库的访问, 作为操作数据库的入口对象sql.DB, 主要为我们提供了两个重要的功能: sql.DB 通过数据库驱动为我们提供管理底层 ...
- linux ps命令用法
-A 列出所有的进程-w 显示加宽可以显示较多的资讯-au 显示较详细的资讯-aux 显示所有包含其他使用者的行程 -A 显示所有进程(等价于-e)(utility)-a 显示 ...
- Springboot中使用Xstream进行XML与Bean 相互转换
在现今的项目开发中,虽然数据的传输大部分都是用json格式来进行传输,但是xml毕竟也会有一些老的项目在进行使用,正常的老式方法是通过获取节点来进行一系列操作,个人感觉太过于复杂.繁琐.推荐一套简单的 ...
- Maven 项目 无缘无故报错:版本冲突,其他机器上正常-提交的时候报冲突怎么也解决不掉
2018年: maven突然之间报错了,显示版本冲突,但是其他的机器是好的, 使用命令:mvn compile -P dev -e; 看看测试环境有没有问题,还是有问题.而且,刚开始只是报错:erro ...
- zsh & tree & macOS
zsh & tree & macOS https://unix.stackexchange.com/questions/22803/counting-files-in-leaves-o ...
- Front-end Job Interview Questions
Front-end Job Interview Questions 前端面试 https://github.com/h5bp/Front-end-Developer-Interview-Questio ...
- dreamweavercs 和dreamweaver cc的區別
https://zhidao.baidu.com/question/1541178469432885667.html