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 ...
随机推荐
- iOS通知传值
NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"loginName"] = @&q ...
- debussy与modelsim的联调设置
前段时间看到网上有人在使用debussy软件对Verilog代码进行调试,而且都称赞其是多么的好用,看着很是馋人,说吧,现在用的是quartus与modelsim的联调,似乎还是可以的,但就是每次稍微 ...
- HDU 1087 Super Jumping! Jumping! Jumping! --- DP入门之最大上升子序列
题目链接 DP基础题 求的是上升子序列的最大和 而不是最长上升子序列LIS DP[i]表示以a[i]结尾所能得到的最大值 但是a[n-1]不一定是整个序列能得到的最大值 #include <bi ...
- HDU 2059 龟兔赛跑 (dp)
题目链接 Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击--赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成 ...
- idea如何搭建springmvc4
1.推荐大牛博客 此操作我操作了三次过后终于成功了,奉献大牛博客连接:做的非常详细到位,望各位采纳,推荐置顶. https://www.cnblogs.com/chenlinghong/p/83395 ...
- linux 下 /bin /sbin 的区别 -- (转)
/bin,/sbin,/usr/bin,/usr/sbin区别 / : this is root directory root 用户根目录 /bin : command ...
- url编码模块
use LWP::SImple; use URI::Escape; encoded_string = uri_escape(raw_string); get(encoded_string);
- Linux信号函数
1. signal函数: #include <signal.h> void (*signal(int signo, void (*func)(int)))(int); ret-成功返回信号 ...
- Linux内核空间内存申请函数kmalloc、kzalloc、vmalloc的区别【转】
转自:http://www.th7.cn/system/lin/201606/167750.shtml 我们都知道在用户空间动态申请内存用的函数是 malloc(),这个函数在各种操作系统上的使用是一 ...
- java===java基础学习(14)---封装
package dog; public class Demo4 { public static void main(String []args) { Worker w1= new Worker(&qu ...