POJ3666 Making the Grade [DP,离散化]
Making the Grade
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9090 | Accepted: 4253 |
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来做。当然蒟蒻DP本来就蒻,讲的可能不太清楚。
根据题意,构造的只能是广义单调数列,那么就考虑单调递增的情况。
如果要让结果尽可能小,那么肯定要求构造的序列中最大的数maxx最小,同时满足每个位置上构造的数x最小。那么设状态转移方程为dp[i][j],表示当前到了第i个位置,序列中最大的数为j,状态转移方程为dp[i][j]=abs(j-w[i])+min(d[i-1][k]) (k<=j)。当然数据的范围太大,需要离散化。但是三重循环复杂度为O(nm^2),那么每次枚举k时直接在j的循环中设置一个minn=min(min,dp[i-1][j]),把方程改为dp[i][j]=min{abs(j-w[i])+minn},可以将复杂度降低到O(nm),离散化以后就是O(n^2)。讲的肯定听不懂,那就看代码吧。
Code:
//It is made by HolseLee on 21st May 2018
//POJ 3666
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#define Fi(i,a,b) for(int i=a;i<=b;i++)
#define Abs(a) ((a)>0?(a):-(a))
using namespace std;
typedef long long ll;
const int N=;
int n,m,a[N],b[N];
ll dp[N][N];
void work()
{
Fi(i,,n){ll mn=dp[i-][];
Fi(j,,n){mn=min(mn,dp[i-][j]);
dp[i][j]=Abs(a[i]-b[j])+mn;}}
ll ans=dp[n][];
Fi(i,,n)ans=min(ans,dp[n][i]);
printf("%lld\n",ans);
}
int main()
{
ios::sync_with_stdio(false);
cin>>n;Fi(i,,n){cin>>a[i];b[i]=a[i];}
sort(b+,b+n+);work();return ;
}
POJ3666 Making the Grade [DP,离散化]的更多相关文章
- POJ3666Making the Grade[DP 离散化 LIS相关]
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6445 Accepted: 2994 ...
- POJ - 3666 Making the Grade(dp+离散化)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- POJ3666 Making the Grade
POJ3666 Making the Grade 题意: 给定一个长度为n的序列A,构造一个长度为n的序列B,满足b非严格单调,并且最小化S=∑i=1N |Ai-Bi|,求出这个最小值S,1<= ...
- 【题解】Making The Grade(DP+结论)
[题解]Making The Grade(DP+结论) VJ:Making the Grade HNOI-D2-T3 原题,禁赛三年. 或许是我做过的最简单的DP题了吧(一遍过是什么东西) 之前做过关 ...
- CF13C Sequence(DP+离散化)
题目描述 给定一个序列,每次操作可以把某个数+1-1.要求把序列变成非降数列.求最少的修改次数. 输入输出样例 输入 #1 - 输出 #1 4 输入 #2 输出 #2 1 解题思路 这题是一道非常好题 ...
- poj3666 Making the Grade(基础dp + 离散化)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- poj3666/CF714E/hdu5256/BZOJ1367(???) Making the Grade[线性DP+离散化]
给个$n<=2000$长度数列,可以把每个数改为另一个数代价是两数之差的绝对值.求把它改为单调不增or不减序列最小代价. 话说这题其实是一个结论题..找到结论应该就很好做了呢. 手玩的时候就有感 ...
- poj3666 Making the grade【线性dp】
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions:10187 Accepted: 4724 ...
随机推荐
- 【BZOJ】1610: [Usaco2008 Feb]Line连线游戏
[算法]计算几何 [题解]计算所有斜率排序去重. 实数判断相等用fabs(...)≤eps. ★斜率题一定要注意斜率不存在的情况!!! 其实我觉得这份代码可以hack的…… #include<c ...
- shell 给未定义的变量设定默认值 ${parameter:-word}
参考: [ Unix & Linux ] Shell Demo $echo ${JENKINS_VERSION:-2.7.4} 2.7.4 $JENKINS_VERSION=2.99 $ech ...
- gridveiw的使用
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- 阿里云服务器部署笔记二(python3、Flask、uWSGI、Nginx)
从git上把项目拉到服务器,项目可以在服务器上运行后,就只需要配置uwsgi和nginx了.它们的逻辑关系是:外部请求->nginx->uwsgi->项目实例. 一.配置uwsgi ...
- kaggle比赛之悟
一.模型与特征哪个重要? 参与Sberbank Russian Housing Market比赛,一开始使用sklearn的岭回归函数Ridge(),残差值一直是0.37左右,然后同样的特征又使用了X ...
- 2017-2018-1 20179205《Linux内核原理与设计》第七周作业
<Linux内核原理与设计>第七周作业 视频学习及操作分析 创建一个新进程在内核中的执行过程 fork.vfork和clone三个系统调用都可以创建一个新进程,而且都是通过调用do_for ...
- python基础===类的私有属性(伪私有)
说在前面的一点: python明明有私有的定义方法就是在变量或者方法的面前加上双下滑线__,这个实际上是python的伪私有.只是一种程序员约定俗称的规定,加了就表示私有变量,但是你如果要在外部调用的 ...
- python基础===继承
编写类时,并非总是要从空白开始.如果你要编写的类是另一个现成类的特殊版本,可使用继承.一个类继承另一个类时,它将自动获得另一个类的所有属性和方法:原有的类称为父类,而新类称为子类.子类继承了其父类的所 ...
- 定制LFS镜像及安装过程
定制LFS镜像及安装过程 http://blog.csdn.net/decload/article/details/7407698 一.定制LFS镜像 定制LFS镜像的思想是在已构建完成 ...
- [转载]Python: 你不知道的 super
原文出处: geekvi super() 的入门使用 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了,可通过使用 ...