Given an array of integers ,the cost to change an element is abs(newvalue — initialvalue) .Determine the minimum cost to sort the array either ascending or descending .
1<=n<=1e3 1<=arr[i]<=1e9
I couldnt find its solution after so much brainstorming can anyone help me out with its solution?
Auto comment: topic has been updated by LIT2021015_AYUSH (previous revision, new revision, compare).
Auto comment: topic has been updated by LIT2021015_AYUSH (previous revision, new revision, compare).
subtract the index i from each element a[i] then find the minimum cost to make the array equal
Well if we have 1,7,5,5 we can choose a[1] and change 7 to 5 with cost of 2 and this is the minimum cost to sort this array .
Can u explain ur approach on this example?
Problem: https://codeforces.com/problemset/problem/713/C
This is now a well-known DP technique called slope trick, see here or here.
Thanks for help .