English 中文(简体)
进行数组排序最小操作数
原标题:Minimum Number of Operations to make an array sorted

I have been trying this problem on spoj, not able to come up with the correct approach. What is the correct algo to solve the problem ?

最佳回答

您应该找到最长的连续递增子序列, 可以在 O(n log n) (通过排序数组) 中完成, 在此之后, 需要的更改数量是 < code> N - 最长的连续递增子序列 。 请注意, 连续的我意指排序数组的顺序 。

例如:

1 7 6 2 5 4 3 => 1-2-3 is longest consecutive increasing subsequence, number of moves needed is 4.

1 6 4 3 5 2 7 => 1-2 or 4-5 or 6-7 is longest consecutive increasing subsequence, note that 1-4-5-7 is longest increasing subsequence but number of moves needed is 5 not 3.

为何如此成功:

最佳算法不会更改某些项目的位置, 调用最大的子序列而不更改为 < code> X < / code >, 您在操作期间不会改变 < code> X < / code > 项的位置, 因此它们应该以递增模式进行排序。 但是由于您只是允许在数组的第一个或最后一个移动某些项目, 您不能在 < code> X < / code > 项之间放置任何项目( 注意我们假设 < code> X 是操作期间最大的未改变的子序列), 所以在操作期间, 不应该在 < code> X < / code> 项之间有间隔。 因此, 它们应该排序和排序格式连续排列 。

因此需要的更改数量不能小于 X < / code > 的 < code> N- 长度, 但也并不难用 X 操作 < / code > 的 < code > N- 长度完成您的工作。

问题回答

暂无回答




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签