To solve this problem, we need to find the minimum number of operations required to transform a given sequence into a non-decreasing sequence using two types of operations: swapping adjacent elements and incrementing an element by 1. The solution involves a greedy approach combined with a Fenwick Tree to efficiently track and compute the necessary swap costs.
Approach
The key insight is to balance the cost of swaps and increments. We process each element while maintaining the current maximum value of the non-decreasing sequence. For each element, we consider two groups:
Elements that can be placed without any increment (value >= current maximum).
Elements that require an increment (value < current maximum).
We use a Fenwick Tree to efficiently compute the number of swaps required to move an element to its position. The goal is to minimize the total cost, which is the sum of swap costs and increment costs.
Solution Code
python
import sys
from heapq import heappush, heappop
def main():
sys.setrecursionlimit(1 << 25)
N = int(sys.stdin.readline())
A = [int(sys.stdin.readline()) for _ in range(N)]
indices = list(range(N))
elements = list(enumerate(A))
elements.sort(key=lambda x: (x[1], x[0]), reverse=True)
fenwick = [0] * (N + 2)
def update(pos, val):
while pos <= N:
fenwick[pos] += val
pos += pos & -pos
def query(pos):
res = 0
while pos > 0:
res += fenwick[pos]
pos -= pos & -pos
return res
for i in range(N):
update(i + 1, 1)
current_max = 0
total_cost = 0
pq = []
ptr = 0
for _ in range(N):
while ptr < N and elements[ptr][1] >= current_max:
original_index = elements[ptr][0]
swap_cost = query(original_index)
heappush(pq, (swap_cost, -original_index, elements[ptr][1], ptr))
ptr += 1
if pq:
min_swap, min_orig, val, idx = heappop(pq)
orig_index = -min_orig
total_cost += min_swap
current_max = val
update(orig_index + 1, -1)
else:
min_cost = float('inf')
selected = None
for j in range(ptr, N):
original_index = elements[j][0]
swap_cost = query(original_index)
inc_cost = current_max - elements[j][1]
total_cost_candidate = swap_cost + inc_cost
if total_cost_candidate < min_cost:
min_cost = total_cost_candidate
selected = j
if selected is not None:
original_index = elements[selected][0]
val = elements[selected][1]
total_cost += min_cost
current_max = current_max
elements[selected], elements[ptr] = elements[ptr], elements[selected]
update(original_index + 1, -1)
ptr += 1
print(total_cost)
if __name__ == "__main__":
main()
Explanation
Fenwick Tree: This data structure is used to efficiently compute the number of elements that need to be swapped to move an element to its correct position.
Priority Queue: This helps in selecting the element with the minimum swap cost from the group of elements that do not require increments.
Greedy Selection: Elements are processed in descending order of their values to ensure that we prioritize elements that can be placed without increments, thus minimizing the need for future increments.
This approach ensures that we efficiently balance the costs of swaps and increments to achieve the minimum total operations required to transform the sequence into a non-decreasing order.