May I ask if someone has already seen or faced the following problem?
I need to handle a list of cost/profit values c 1 /p 1 , c 2 /p 2 , c 3 /p 3 ,... that satisfies:
This is an example: 2/3
, 4/5
, 9/15
, 12/19
If one tries to 10/14
in above list, the operation is because of the existing cost/profit pair 9/12
: it is never useful to increase the cost ( 9->10
) and decrease the profit ( 14->12
). 10/14
,则由于现有成本/利润对9/12
而了该操作:增加成本( 9->10
)和降低利润( 14->12
)。 Such lists can arise for instance in (the states of) dynamic programming algorithms for knapsack problems, where the costs can represent weights.
If one 7/20
in above list, this should of 9/15
and 12/19
. 7/20
,则应 9/15
和12/19
。
I have written a solution using the C++
std::set
(often implemented with red-black trees), but I needed to provide a comparison function that eventually become a bit overly complex. Also, the insertion in such sets takes logarithmic time and that can easily actually lead to linear time (in terms of non-amortized complexity) for example when an insertion triggers the deletion of all other elements.
I wonder if better solutions exist, given that there are countless solutions to implement (ordered) sets, eg, priority queues, heaps, linked lists, hash tables, etc.
This is a Pareto front (obj1: min cost, obj2: max profit) , but I still could not find the best structure to record it.
I did not fully understand the rules you described, so I will agnostically say that an attempt to an insertion might trigger rejection and if it is accepted, then subsequent items need to be removed.
You will need to use a balanced comparison tree, represented as an array. In that case, finding the nodes you need will take O(logN) time, which will be the complexity of a search or a rejected insertion attempt. When you need to remove items, then you remove them and insert a new one, which has a complexity of
O(logN + N + N + logN) (that is, searching, removing, rebalancing and inserting. We could get rid of the last logarithm if while rebalancing we knoe where the new item is to be inserted)
O(logN + N + N + logN) = O(2logN + 2N) = O(logN^2 + 2N), which is largely a linear complexity.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.