Making the Grade_滚动数组&&dp

时间:2023-01-23 09:17:56

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

|AB1| + |AB2| + ... + |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[i][j]=min(dp[i-1][j]|1<=j<=n)+abs(a[i]-b[j]);

dp[i][j]前i个数,最大为b[j]时的最小代价

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#define inf 0x7fffffff
#define ll long long
#define get_abs(a) ((a)>0?(a):-(a))
using namespace std;
const int N=;
ll a[N],b[N];
long long int dp[N][N]; int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
memset(dp,,sizeof(dp));
ll minx,ans=inf;
for(int i=; i<=n; i++)
{
minx=dp[i-][];
for(int j=; j<=n; j++)
{
minx=min(minx,dp[i-][j]);
dp[i][j]=minx+get_abs(a[i]-b[j]);
}
}
for(int i=; i<=n; i++)
{
ans=min(ans,dp[n][i]);
}
cout<<ans<<endl;
}
return ;
}

Making the Grade_滚动数组&&dp的更多相关文章

  1. Palindrome&lowbar;滚动数组&amp&semi;&amp&semi;DP

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  2. poj - 1159 - Palindrome(滚动数组dp)

    题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->&gt ...

  3. HDU 4576 简单概率 &plus; 滚动数组DP(大坑)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 坑大发了,居然加 % 也会超时: #include <cstdio> #includ ...

  4. Gym 100507G&Tab;The Debut Album (滚动数组dp)

    The Debut Album 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/G Description Pop-group & ...

  5. 【滚动数组】 dp poj 1036

    题意:一群匪徒要进入一个酒店.酒店的门有k+1个状态,每个匪徒的参数是:进入时间,符合的状态,携带的钱. 酒店的门刚开始状态0,问最多这个酒店能得到的钱数. 思路: dp数组为DP[T][K]. 转移 ...

  6. BZOJ-1925 地精部落 烧脑DP&plus;滚动数组

    1925: [Sdoi2010]地精部落 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1053 Solved: 633 [Submit][Status ...

  7. HDU 1024 Max Sum Plus Plus --- dp&plus;滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  8. &lbrack;POJ1159&rsqb;Palindrome(dp,滚动数组)

    题目链接:http://poj.org/problem?id=1159 题意:求一个字符串加多少个字符,可以变成一个回文串.把这个字符串倒过来存一遍,求这两个字符串的lcs,用原长减去lcs就行.这题 ...

  9. Codeforces 712 D&period; Memory and Scores &lpar;DP&plus;滚动数组&plus;前缀和优化&rpar;

    题目链接:http://codeforces.com/contest/712/problem/D A初始有一个分数a,B初始有一个分数b,有t轮比赛,每次比赛都可以取[-k, k]之间的数,问你最后A ...

随机推荐

  1. MyEclipse for linux 破解方法

    1.安装MyEclipse: uu@pc:~/desktop$ chmod +x myeclipse-pro-2014-GA-offline-installer-linux.run uu@pc:~/d ...

  2. php 处理递归提成的方案

    好久没有写blog了,最近CRM项目中用到了递归提成的方案 CREATE TABLE `crm_proxy_bonux_rule` ( `id` ) NOT NULL AUTO_INCREMENT C ...

  3. 什么是JDK

    Java Development Kit (JDK) 是太阳微系统针对Java开发人员发布的免费软件开发工具包(SDK,Software development kit).JDK 是整个Java的核心 ...

  4. DEDE织梦,文章页里的幻灯调用,能调用全部栏目的吗

    arclist 是必须要有 typeid 设置的,如果你没设置,默认是取的当前栏目的 typeid,而首页取到的是 top,所以你强制指定typeid=top就可以了.---------------- ...

  5. wordpress如何删除没有文章的tags标签

    wordpress站点除了可以按博客category分类外,还可以在写文章的时候适当添加tags标签(当然,if you are lazy,哈哈,可以安装auto tag插件来实现),发布的posts ...

  6. SQL数据库还原时备份集中的数据库备份与现有的数据库不同的解决办法

    SQL Server 2005数据库还原出错错误具体信息为:备份集中的数据库备份与现有的A数据库不同 具体操作如下:第一次:新建了数据库A,数据库文件放在E:\DB\A目录下,选中该数据库右键-任务- ...

  7. vijosP1210 盒子与球

    vijosP1210 盒子与球 链接:https://vijos.org/p/1210 [思路] Stirling+全排列. 因为第二类stirling所求是没有标明盒子顺序的方案数,所以最后需要乘一 ...

  8. 【网络爬虫入门05】分布式文件存储数据库MongoDB的基本操作与爬虫应用

    [网络爬虫入门05]分布式文件存储数据库MongoDB的基本操作与爬虫应用 广东职业技术学院  欧浩源 1.引言 网络爬虫往往需要将大量的数据存储到数据库中,常用的有MySQL.MongoDB和Red ...

  9. mysql批量数据脚本

    mysql批量数据脚本 1 建表 create table dept( id int unsigned primary key auto_increment, deptno mediumint uns ...

  10. Spring定时器配置与运用,及Cron表达式的详解

    一:首先在spring的配置文件里配置一个定时器 <task:executor id="executor" pool-size="5" /> &lt ...