简体   繁体   English

动态规划最优“非递减序列”

[英]Dynamic Programming Optimal “Non-Decreasing Sequence”

The question says, 问题说,

Given an array A with N integer numbers, output the minimum number of operations needed to make the sequence non decreasing. 给定具有N个整数的数组A,输出使序列不减少所需的最小操作数。

An operation represents choosing a number in the array A[i], summing it to A[i + 1] or A[i - 1], and deleting A[i] 操作表示选择数组A [i]中的数字,将其相加为A [i + 1]或A [i - 1],并删除A [i]

The link to the problem ( spanish ): https://omegaup.com/arena/problem/Torres#problems 问题的链接(西班牙语): https//omegaup.com/arena/problem/Torres#problems

Example: 例:

3
5 2 1

Answer: 2

In this case we must join all the numbers, to turn the sequence to { 8 }, which is non-deceasing 在这种情况下,我们必须连接所有数字,将序列转换为{8},这是非死亡的

Limits: 限制:

N <= 5000
A[i] <= 10^5

I think this problem can be solved using DP, but i can't discover a state that can represent the problem in a small and correct way. 我认为这个问题可以用DP来解决,但是我无法发现一个可以用小而正确的方式表示问题的状态。

Thanks in advance. 提前致谢。

EDIT: I was wrong. 编辑: 我错了。 . The following algorithm doesn't solve the problem. 以下算法无法解决问题。

It can be done in one linear scan from left to right. 它可以从左到右进行一次线性扫描。 Let me explain my reasoning: 让我解释一下我的推理:

If you join two numbers A[i] and A[i+1] , the result is greater than A[i] . 如果连接两个数字A[i]A[i+1] ,则结果大于A[i] If the part left of A[i] is already non-decreasing, this operation is necessary if A[i] < A[i-1] . 如果A [i]左边的部分已经非递减,则如果A[i] < A[i-1]则需要此操作。

Unnecessarily joining A[i] and A[i+1] consumes an operation and makes things more difficult right of the join, so we do it only if necessary. 不必要地加入A[i]A[i+1]会消耗操作并使连接更加困难,所以我们只在必要时才这样做。

  • If A[i] < A[i-1] , join A[i] and A[i+1] until A[i] >= A[i-1] . 如果A[i] < A[i-1] ,加入A[i]A[i+1]直到A[i] >= A[i-1]
  • If A[i] >= A[i-1] , increment i. 如果A[i] >= A[i-1] ,则递增i。

PS The original problem description states that A[i] is in the range 1...10^5 , thus explicitly excluding negative numbers, which is important for the algorithm. PS原始问题描述表明A[i]1...10^5的范围内,因此明确排除负数,这对算法很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM