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 ...
随机推荐
- [洛谷P1822] 魔法指纹
洛谷题目连接:魔法指纹 题目描述 对于任意一个至少两位的正整数n,按如下方式定义magic(n):将n按十进制顺序写下来,依次对相邻两个数写下差的绝对值.这样,得到了一个新数,去掉前导0,则定义为ma ...
- bzoj 1700: [Usaco2007 Jan]Problem Solving 解题 ——dp
Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地说,他们有P (1 <= P <= 300) 道题目要做. 他们还离开了农场 ...
- PHP is_null,empty以及isset,unset的区别
1.empty 判断一个变量是否为“空”.null.false.00.0.’0′.』.为以上值的变量在检测時都将返回true. 2.isset 判断一个变量是否已经设置.0.00.’0′.』.’ ‘. ...
- 【洛谷 P1525】 关押罪犯 (二分图+二分答案)
题目链接 并查集+贪心当然是可以做的. 但我用二分图+二分答案. 二分一个\(mid\),删去所有边权小于等于\(mid\)的边,看有没有奇环存在,如果存在,则\(mid\)不行. #include ...
- Python 关于拷贝(copy)汇总(列表拷贝 // 字典拷贝 // 自定义对象拷贝)
1.列表拷贝 引用是指保存的值为对象的地址.在 Python 语言中,一个变量保存的值除了基本类型保存的是值外,其它都是引用,因此对于它们的使用就需要小心一些.下面举个例子: 问题描述:已知一个列表, ...
- LCD实验学习笔记(七):NAND FLASH
s3c2440 CPU内置NAND FLASH控制器.相关寄存大器起始地址为0x4e000000. 通过设置NFCONF寄存器,设置NAND FLASH 时序. 通过设置NFCONT寄存器,使能NAN ...
- 【转】linux下杀死进程
经过搜集和整理相关的Linux操作系统杀死进程的材料,在这里给大家推荐本篇文章,希望大家看后会有不少收获. 1. kill 作用:根据进程号杀死进程 用法: kill [信号代码] 进程ID 举例: ...
- 去掉每行的特定字符py脚本
百度下载一个脚本的时候遇到那么一个情况.每行的开头多了一个空格.https://www.0dayhack.com/post-104.html 一个个删就不要说了,很烦.于是就有了下面这个脚本. #! ...
- python自动开发之第二十五天
一.组合搜索 参考: http://www.cnblogs.com/ccorz/p/5985205.html 二.JSONP 1.在同源策略下,在某个服务器下的页面是无法获取到该服务器以外的数据的,但 ...
- WmiPrvSE.exe进程(WMI Provider Host)不能删除
WmiPrvSE.exe进程基本信息:程序厂商:微软® Microsoft Corp.进程描述:WMI Provider Host进程属性:Windows系统进程使用网络:是的启动情况:触发启动 来历 ...