poj3666 Making the Grade(基础dp + 离散化)
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.
题意:给你一个序列,求最少的代价让其变成单调不递增序列或单调不递减序列。
看到这道题目应该会想到用dp处理,dp[i][max]表示处理到前i位的最大值位max,于是只要两个for就可以,i=1~n,j=1~max。
dp[i][j]=min(dp[i][1~j]) + abs(a[i]-j),然后再从dp[n][1~max]中找最小的。
这题数据有点大如果用max会TLE,所以要将数据离散化一下。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define Abs(a) ((a)>0?(a):-(a))
using namespace std;
typedef long long ll;
const int M = 2e3 + 10;
ll dp[M][M];
ll a[M] , b[M] , c[M];
int main()
{
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i];
c[n + 1 - i] = a[i];
b[i] = a[i];
}
sort(b + 1 , b + n + 1);
for(int i = 1 ; i <= n ; i++) {
ll MIN = dp[i - 1][1];
for(int j = 1 ; j <= n ; j++) {
MIN = min(MIN , dp[i - 1][j]);
dp[i][j] = MIN + Abs((a[i] - b[j]));
}
}
ll ans = dp[n][1];
for(int i = 1 ; i <= n ; i++) {
ans = min(ans , dp[n][i]);
}
memset(dp , 0 , sizeof(dp));
for(int i = 1 ; i <= n ; i++) {
ll MIN = dp[i - 1][1];
for(int j = 1 ; j <= n ; j++) {
MIN = min(MIN , dp[i - 1][j]);
dp[i][j] = MIN + Abs((c[i] - b[i]));
}
}
for(int i = 1 ; i <= n ; i++) {
ans = min(ans , dp[n][i]);
}
cout << ans << endl;
return 0;
}
poj3666 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 elevation more tha ...
- poj3666/CF714E/hdu5256/BZOJ1367(???) Making the Grade[线性DP+离散化]
给个$n<=2000$长度数列,可以把每个数改为另一个数代价是两数之差的绝对值.求把它改为单调不增or不减序列最小代价. 话说这题其实是一个结论题..找到结论应该就很好做了呢. 手玩的时候就有感 ...
- poj 3666 Making the Grade(dp离散化)
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7068 Accepted: 3265 ...
- [poj3666]Making the Grade(DP/左偏树)
题目大意:给你一个序列a[1....n],让你求一个序列b[1....n],满足 bi =a && bc,则最小的调整可以是把b变成c. 所以归纳可知上面结论成立. dp[i][j] ...
- 「kuangbin带你飞」专题十二 基础DP
layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathj ...
- 基础dp
队友的建议,让我去学一学kuangbin的基础dp,在这里小小的整理总结一下吧. 首先我感觉自己还远远不够称为一个dp选手,一是这些题目还远不够,二是定义状态的经验不足.不过这些题目让我在一定程度上加 ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- 基础DP(初级版)
本文主要内容为基础DP,内容来源为<算法导论>,总结不易,转载请注明出处. 后续会更新出kuanbin关于基础DP的题目...... 动态规划: 动态规划用于子问题重叠的情况,即不同的子问 ...
- hdu 5586 Sum 基础dp
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Desc ...
随机推荐
- 角度转弧度&根据弧度计算圆周上点的坐标的方法
角度转弧度: #define AngleToRadian(angle) (M_PI/180.0f)*angle 以正东面为0度起点计算指定角度所对应的圆周上的点的坐标: float radian = ...
- 树莓派 + Windows IoT Core 搭建环境监控系统
前言:Windows IoT 是微软为嵌入式开发板设计的一种物联网操作系统,运行Windows UWP(C# 开发),可以设计出丰富的交互界面,驱动GPIO,连接一些传感器做有意思的事,本文详细介绍如 ...
- 案例实战:每日上亿请求量的电商系统,JVM年轻代垃圾回收参数如何优化?
出自:http://1t.click/7TJ 目录: 案例背景引入 特殊的电商大促场景 抗住大促的瞬时压力需要几台机器? 大促高峰期订单系统的内存使用模型估算 内存到底该如何分配? 新生代垃圾回收优化 ...
- 微信JSSDK签名
微信JS-SDK说明文档 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 生成签名 1.签名规则 参与签名的 ...
- 12、面向对象的思想(OOP)
面向对象与面向过程 1.都是解决问题的思维方式,都是代码的组织的方式: 2.解决简单的问题可以使用面向过程: 3.解决复杂的问题建议使用面向对象,微观处理依旧会使用面向过程. 对象的进化史(数据管理的 ...
- 消息中间件-activemq实战之消息持久化(六)
对于activemq消息的持久化我们在第二节的时候就简单介绍过,今天我们详细的来分析一下activemq的持久化过程以及持久化插件.在生产环境中为确保消息的可靠性,我们肯定的面临持久化消息的问题,今天 ...
- LeetCode——540. Single Element in a Sorted Array
题目:Given a sorted array consisting of only integers where every element appears twice except for one ...
- viewpager_轮播
public class MainActivity extends Activity { private ViewPager pager; private int[] id={R.layout.lay ...
- yolo v2
https://blog.csdn.net/wfei101/article/details/79398563 https://blog.csdn.net/oppo62258801/article/de ...
- Spring MVC内容协商实现原理及自定义配置【享学Spring MVC】
每篇一句 在绝对力量面前,一切技巧都是浮云 前言 上文 介绍了Http内容协商的一些概念,以及Spring MVC内置的4种协商方式使用介绍.本文主要针对Spring MVC内容协商方式:从步骤.原理 ...