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 个数中,最大 ...
随机推荐
- jquery将form表单序列化常json
var formData = {};$.each(form.serializeArray(),function(i, item){ formData[item.name] = item.value;} ...
- 一个简单的数据增量更新策略(Android / MongoDB / Django)
我在做个人APP - CayKANJI - 的时候遇到一个问题: 如何增量式地把日语汉字数据地从server更新到APP端,即每次用户运行更新操作时,仅仅获取版本号高于本地缓存的内容. 数据格式 为了 ...
- git入门五(分支合并冲突和衍合)
分支合并冲突的处理 合并分支的冲突时在不同的分支中修改了同一个文件的同一部分,程序无法把两份有差异的文件合并,这时候需要人为的干预解决冲突.当前处于master 分支,当dev 分支和master ...
- horizontalDragMaxWidth:0;就没有水平滚动条了
jquery.jscrollpane.css JScrollPane工作所必须的基本的CSS样式.jquery.min.jsjQuery作为javascript库必须提前引入.jquery.mouse ...
- UGUI随记
<color=#ffef00ff>武器</color>:巨剑 <color=#ffef00ff>种族</color>:人族 <color=#ffe ...
- 【BZOJ】2186 沙拉公主的困惑
一道很有价值的题. [解析1]欧几里德算法求乘法逆元,前缀和 [Analysis]O(T n log n). [Sum] ①int运算.假设会超出界,第一个数前要加上(LL)即类型转换. ②gcd不变 ...
- 【BZOJ4154】[Ipsc2015]Generating Synergy KDtree
[BZOJ4154][Ipsc2015]Generating Synergy Description 给定一棵以1为根的有根树,初始所有节点颜色为1,每次将距离节点a不超过l的a的子节点染成c,或询问 ...
- android启动页延时跳转
package com.goodness.goodness; import android.content.Context; import android.content.Intent; import ...
- centos7 mysql允许远程连接设置
Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下操作: 一.允许root用户在任何地方进行远程登录,并具有所有库 ...
- 4django模板
在前面的几节中我们都是用简单的django.http.HttpResponse来把内容显示到网页上,本节将讲解如何使用渲染模板的方法来显示内容 1.创建一个 zqxt_tmpl 项目,和一个 名称为 ...