poj 3666 Making the Grade(dp离散化)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7068 | Accepted: 3265 |
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。
1.
对前i个序列,构成的最优解其实就是与两个参数有关。一个是这个序列处理后的最大值mx,和这个序列处理的代价值cost。
显然最大值mx最小最好(这样第i+1个值可以不花代价直接接在其后面的可能性更大),cost最小也最好(题意要求),但是两者往往是鱼和熊掌。
用dp[i][j]表示:前i个数构成的序列,这个序列最大值为j,dp[i][j]的值代表相应的cost。
所以状态转移方程如下:
dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k<=j)

这个表格是根据转移方程写出来的dp数组。
再仔细看一下转移方程:dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k<=j)
右边没填充的是因为填充的数字肯定比前面的数字大,无用,因为在求min( dp[i-1][k] )时,是求最小值,既然更大,则最小值时无需考虑。
又从表格中可以看出:
dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k<=j)这里的k无需从1遍历到j。
只要在对j进行for循环的时候不断更新一个dp[i-1][j]的最小值mn=min(mn,dp[i-1][j]),
然后对dp[i][j]=abs(j-w[i])+mn即可;
这样改进之后即可从本来的时候时间复杂度O(NMM)改进为O(NM);
但是,这里的m是A[i]的最大值,显然TLE。
所以必须用离散化思想改进,因为N=2000。远小于A[i]的最大值。
离散化:将序列排序一下,然后用位置的前后关系来制定其值,这样时间复杂度变成O(N^2).
最后是这题数据有bug,只需要求不减序列即可。
?:谁能给我讲讲为什么这样离散化?为什么最后的结果中的每一个的数字都在原来的串中出现过?为什么这样得到的就是一个最优解?
附个解题报告,没太看懂他的证明
http://www.cnblogs.com/vb4896/p/5877962.html
#include<iostream>
#include <cstdio>
#include<vector>
#include<algorithm>
#define Abs(a) ((a)>0?(a):-(a))
#define Mod(a,b) (((a)-1+(b))%(b)+1)
using namespace std;
const int N=;
const long long inf=(<<);
int n;
int a[N],b[N];
long long int dp[N][N];
void solve()
{
for(int i=;i<=n;i++)
{
long long mn=dp[i-][];
for(int j=;j<=n;j++)
{
mn=min(mn,dp[i-][j]);
dp[i][j]=Abs(a[i]-b[j])+mn;
}
}
long long ans=dp[n][];
for(int i=;i<=n;i++)
ans=min(ans,dp[n][i]);
printf("%lld\n",ans);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",a+i);
b[i]=a[i];
}
sort(b+,b+n+);
solve(); return ;
}
//二维解法
#include<iostream>
#include <cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=; int n;
__int64 a[N];
__int64 b[N]; bool cmp(__int64 a,__int64 b)
{
return a<b;
} __int64 dp[N][N];
__int64 min(__int64 x,__int64 y)
{
return x<y?x:y;
}
int main()
{
//freopen("in.txt","r",stdin);
__int64 i,j,k,ans;
cin>>n;
for(i=; i<=n; i++) {
scanf("%I64d",&a[i]);
}
for(i=; i<=n; i++) {
b[i]=a[i];
}
sort(b+,b++n,cmp);
for(j=; j<=n; j++) {
dp[][j]=a[]-b[j];
if(dp[][j]<) {
dp[][j]=-dp[][j];
}
}
__int64 t;
for(i=; i<=n; i++) { // 非严格递增
k=dp[i-][];
for(j=; j<=n; j++) {
k=min(dp[i-][j],k);
t=a[i]-b[j];
if(t<) {
t=-t;
}
dp[i][j]=k+t; //dp[i][j] = min{dp[i - 1][0...j] + ?abs(a[i] - b[j])}
}
}
ans=dp[n][n];
for(i=n; i>=; i--) {
ans=min(ans,dp[n][i]);
}
printf("%I64d\n",ans);
return ;
}
//一维 (滚动数组)
#include<iostream>
#include <cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=;
__int64 a[N],b[N],dp[][N];
__int64 min(__int64 x,__int64 y)
{
return x<y?x:y;
}
int main()
{
int n,i,j;
__int64 k,t;
scanf("%d",&n);
for(i=; i<=n; i++) {
scanf("%I64d",&a[i]);
}
for(i=; i<=n; i++) {
b[i]=a[i];
}
sort(b+,b++n);
for(i=; i<=n; i++) {
dp[][i]=a[]-b[i];
if(dp[][i]<) {
dp[][i]=-dp[][i];
}
}
for(i=; i<=n; i++) {
k=dp[i%][];
for(j=; j<=n; j++) {
k=min(k,dp[i%][j]);
t=a[i]-b[j];
if(t<) {
t=-t;
}
dp[(i+)%][j]=k+t;
}
}
j=(n+)%;
k=dp[j][];
for(i=; i<=n; i++) {
k=min(k,dp[j][i]);
}
printf("%I64d\n",k);
return ;
}
poj 3666 Making the Grade(dp离散化)的更多相关文章
- [poj 3666] Making the Grade (离散化 线性dp)
今天的第一题(/ω\)! Description A straight dirt road connects two fields on FJ's farm, but it changes eleva ...
- 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)
题目链接: 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)
传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total ...
- 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 : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) T ...
- POJ 3666 Making the Grade(二维DP)
题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...
- POJ 3666 Making the Grade (DP)
题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了. 析:并不会做,知道是DP,但就是不会,菜....d[i][j]表示前 i 个数中,最大 ...
随机推荐
- iOS引用当前显示的UIAlertView
UIAlertView在iOS里和一般的UIView不一样,有时候使用起来会有一些不便.特别要引用当前显示的UIAlertView的时候,就存在一些难度. 在iOS7以前,可以下面的代码可以解决这个问 ...
- Harvard数据库课程CS 265: Research Topics in Database Systems
CS 265: Research Topics in Database Systems Announcements Quiz 3 will be posted. Good luck! Quiz 2 h ...
- css3 jQuery实现3d搜索框+为空推断
<!DOCTYPE html> <html> <head> <title>css3实现3d搜索框</title> <style> ...
- Sphinx(Coreseek)安装和使用指南
1.安装 1.1安装mmseg ./bootstrap # 必须执行,不然安装会失败 ./configure --prefix=/usr/local/mmseg- #指定安装目录 make make ...
- linux下LAMP环境搭建
++++++++++++++++++++++++++++++++++++++++++++++ linux下LAMP环境搭建 ++++++++++++++++++++++++++++++++++++++ ...
- android 软键盘监听显示和隐藏
githup中找到:https://github.com/yescpu/KeyboardChangeListener import android.app.Activity; import andro ...
- cmake实战第一篇:初试 cmake
1.准备工作: 首先,在/code_test 目录下建立一个 cmake 目录,用来放置我们学习过程中的所有练习.(如果以下命令出现xxx: cannot create directory ‘x’: ...
- MongoDBTemplate多条件查询的问题
问题: 在使用Spring Data MongoDB 进行条件查询数据时,发现条件判断不起作用,结果会返回所有的数据. Criteria criteria = new Criteria(); crit ...
- hdu 4902 Nice boat(线段树区间改动,输出终于序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 Problem Description There is an old country and ...
- mybatis注解实现CURD
我们来看下面这段代码: /** * The user Mapper interface. * * @author Wangzun * * @version 1.0 * * */ @CacheNames ...