文章标题

简介:

Making the Grade 
Time Limit: 1000MS Memory Limit: 65536K 
Total Submissions: 4732 Accepted: 2244 
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









Sample Output


Source

/* 
题意:修改一些数据,使之非严格递增 (1 2 3 3 4 )(总的趋势是增,能够有同样数)

思路:dp[i][j] 代表前i个数改成满足题意,而且第i个数为第j小的最小消耗值 
那么dp[i][j]=min(dp[i-1][k]+a[i]-b[j]) (b[j])代表排序后的第j小 
当中1<=k<=j;然后发现能够用滚动数组 
*/

include

include

include

include

include

include

include

include

include

include

include

typedef __int64 ll;

define L(x) (x<<1)

define R(x) (x<<1|1)

define MID(x,y) ((x+y)>>1)

using namespace std;

define INF 0x3f3f3f3f

define N 2005

ll dp[2][N];

int a[N],b[N]; 
int n;

int main() 

int i,j; 
while(~scanf(“%d”,&n)) 

for(i=1;i<=n;i++) 

scanf(“%d”,&a[i]); 
b[i]=a[i]; 

sort(b+1,b+n+1); 
for(i=1;i<=n;i++) 
dp[0][i]=fabs(a[1]-b[i]); 
int now=0; 
for(i=2;i<=n;i++) 

__int64 temp=dp[now][1]; //temp记录dp[i-1][1~j]的最小值 
for(j=1;j<=n;j++) 

temp=min(temp,dp[now][j]); 
dp[now^1][j]=temp+abs(a[i]-b[j]); 

now=now^1; 

__int64 ans=dp[now][1];

    for(i=2;i<=n;i++)
        if(dp[now][i]<ans) ans=dp[now][i];
    printf("%I64d\n",ans);

}

return 0; 
}








本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5261599.html,如需转载请自行联系原作者

相关文章
|
5天前
|
存储 人工智能 安全
AI 越智能,数据越危险?
阿里云提供AI全栈安全能力,为客户构建全链路数据保护体系,让企业敢用、能用、放心用
|
8天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
7天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
448 93
|
1天前
|
开发者
「玩透ESA」ESA启用和加速-ER在加速场景中的应用
本文介绍三种配置方法:通过“A鉴权”模板创建函数并设置触发器路由;在ESA上配置回源302跟随;以及自定义响应头。每步均配有详细截图指引,帮助开发者快速完成相关功能设置,提升服务安全性与灵活性。
286 2
|
7天前
|
SQL 人工智能 自然语言处理
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
随着生成式AI的普及,Geo优化(Generative Engine Optimization)已成为企业获客的新战场。然而,缺乏标准化流程(Geo优化sop)导致优化效果参差不齐。本文将深入探讨Geo专家于磊老师提出的“人性化Geo”优化体系,并展示Geo优化sop标准化如何帮助企业实现获客效率提升46%的惊人效果,为企业在AI时代构建稳定的流量护城河。
406 156
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
|
7天前
|
数据采集 缓存 数据可视化
Android 无侵入式数据采集:从手动埋点到字节码插桩的演进之路
本文深入探讨Android无侵入式埋点技术,通过AOP与字节码插桩(如ASM)实现数据采集自动化,彻底解耦业务代码与埋点逻辑。涵盖页面浏览、点击事件自动追踪及注解驱动的半自动化方案,提升数据质量与研发效率,助力团队迈向高效、稳定的智能化埋点体系。(238字)
311 158