Minimum amount of swaps needed so no number has two neighbours that are both greater..?

Viewed 209

The problem statement goes like this: Given a list of N < 500 000 distinct numbers, find the minimum number of swaps of adjacent elements required such that no number has two neighbours that are both greater. A number can only be swapped with a neighbour.
Hint given: Use a segment tree or a fenwick tree.

I don't really get the idea of how I should use a sum-tree to solve this problem.

Example inputs:

Input 1: 
5 (amount of elements in the list)
3 1 4 2 0

output 1: 1

input 2: 
6
4 5 2 0 1 3

output 2: 4
1 Answers
Related