今天的第一题(/ω\)!

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

Source

USACO 2008 February Gold

code:

//By Menteur_Hxy
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std; const int MAX=2010;
const int INF=0x3f3f3f3f;
int n,a[MAX],num[MAX],f[MAX][MAX]; int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
num[i]=a[i];
}
sort(num+1,num+n+1);
int m=unique(num+1,num+n+1)-num-1;
memset(f,0x3f,sizeof f);
f[0][0]=0;
for(int i=1;i<=n;i++) {
int temp=f[i-1][0];
for(int j=1;j<=m;j++) {
temp=min(temp,f[i-1][j]);
f[i][j]=temp+abs(a[i]-num[j]);
}
}
int ans=INF;
for(int i=1;i<=m;i++) ans=min(ans,f[n][i]);
printf("%d",ans);
return 0;
}

最新文章

  1. 【监听文件 多线程】使用java--WatchService监听文件 开启多线程copy文件
  2. 修改NLS_DATE_FORMAT的四种方式
  3. HP “云图”GPU虚拟化工作站解决方案
  4. [CentOs7]安装mysql(2)
  5. css学习笔记(6)
  6. 转:Java NIO系列教程(八) DatagramChannel
  7. R语言描述性统计常用函数
  8. query attr prop区别
  9. 利用python yielding创建协程将异步编程同步化
  10. C#中的预处理器指令
  11. JAVA Useful Program(1)
  12. Vim配置及使用笔记
  13. 递归回溯 UVa140 Bandwidth宽带
  14. 由于DG Broker的配置导致RAC某实例无法mount
  15. python3 字符串str 教程
  16. [转]微擎人人商城m()函数调用model方法
  17. Centos7系统下编写systemd脚本设置redis开机自启动
  18. PCA和SVD最佳理解
  19. sorted()排序详解
  20. JBPM使用方法、过程记录

热门文章

  1. asp.net--OnAuthorization方法
  2. Go在Ubuntu 14.04 64位上的安装过程
  3. [DLX精确覆盖] hdu 3663 Power Stations
  4. Oracle中如何判断字符串是否全为数字
  5. 每天一个linux命令(01):ifconfig命令
  6. php 判断字符串包含中文(转)
  7. Codeforces Round #512 (Div. 2) D.Vasya and Triangle 数学
  8. kafka+storm 单机运行
  9. PLSQL简介
  10. java selenium手动最大化chrome浏览器的方法