O (NM) multi-pack algorithm optimized with monotone queue

Source: Internet
Author: User

I searched for a bit, found a good blog, said very detailed: link.

Analytical

The most primitive state transfer equation for multiple backpacks:

Make c[i] = min (Num[i], j/v[i])

F[I][J] = max (F[i-1][j-k*v[i]] + k*w[i]) (1 <= k <= C[i]) k here refers to the first item of the article I k pieces.

If you make a = J/v[i], B = J V[i] Then J = A * V[i] + b.

Here the meaning of K is changed, k means that the number of items to take the article I is less than a few.

Then f[i][j] = Max (F[i-1][b+k*v[i]]-k*w[i]) + A*w[i] (A-c[i] <= K <= a)

It can be found that f[i-1][b+k*v[i]]-k*w[i] is only related to K, and this k is a continuous one. All we have to do is find out the maximum value of f[i-1][b+k*v[i]]-k*w[i] When K is within the feasible interval.

This enables the use of Monotonic queue optimization.

Code

O (NM) multi-pack algorithm optimized with monotone queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.