简体   繁体   中英

Algorithm: Buying a collection of goods with vouchers

I'm struggling to come up with a solution for this problem for an algorithms course:

You go to a store and want to buy n = {n 1 , n 2 , ..., n n } goods, where the items can be different or not.

The store has the following promotion: "If a customer buys two articles whose prices add up to a value which ends with 11, 33 or 55 cents, he will receive a voucher, worth the corresponding cent value."

The problem is to come up with an algorithm which computes an optimal strategy for buying a given collection of goods, where a minimization of the total cost is wanted.

For example: If you need to buy 3 products (n 1 , n 2 , n 3 ) with prices (1.01$, 2.10$ and 3$), you should buy n 1 and n 2 together and buy n 3 separately, yielding a total cost of: (1.01 + 2.10) + 3 - (0.11) = 6$.

No hint is given, but I think I can use some approach of flow-networks.

Sort items you want to buy by cent prices... ie (25.01$, 1.02$, ..., 0.99$).

And then check... whether any of the cents sums up to 0.99$... This can be solved easily by two sum problem... You take .01$ and look for its compliment 0.98$ using binary search... Delete this pair and repeat until you find no pair of products with cents that sum up to 0.99$.

If this is done, delete found pairs of products... calculate your gain and check whether any of the cents sums up to 0.88$... The same way.

...

If this is done, delete found pairs of products... and check whether any of the cents sums up to 0.11$... The same way.

This is all O(nlogn) 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM