询问该背包变体问题名称及满足约束的N分配方案
Hey there! Let's break down your problem step by step—first working out the exact allocation method, then pinning down which knapsack variant this falls under.
一、具体分配步骤
Since every allocated portion must be a multiple of p, we can simplify the problem to integer-level math to make things easier:
Convert to integer units
Divide all values bypto reframe the problem in terms of integer counts:- Let
M = N / p(sinceNis divisible byp,Mwill be an integer) - For each demand
d[k], calculater[k] = d[k] / p(this might be a decimal)
Now our goal is to split the integerMintokintegersx[0], x[1], ..., x[k-1]such that their sum equalsM, and eachx[k]is as close as possible tor[k](the final allocated quantity will bex[k] * p).
- Let
Calculate base values and adjust for exact sum
- Start with the integer part of each
r[k]as the base:base[k] = floor(r[k])(or just useint(r[k])to grab the whole number part) - Compute the sum of these base values:
sum_base = sum(base[k]). The differencedelta = M - sum_basewill be a non-negative integer (since the total ofd[k]equalsN, the total ofr[k]equalsM). - Next, look at the decimal part of each
r[k] - base[k]. We'll distribute thedeltaextra1s to thedeltaelements with the largest decimal parts. This ensures the total deviation from the original demands (whether measured by absolute difference or squared difference) is minimized, keeping our allocation as close to the target as possible.
- Start with the integer part of each
Example with non-integer demands
Let's say N=300, p=5 (so M=60), and your demands are d[k]=(127, 122, 51):
r[k]becomes(25.4, 24.4, 10.2)base[k]is(25, 24, 10), sum is59, sodelta=1- The decimal parts are
0.4, 0.4, 0.2. We can add the extra1to either of the first two elements, giving usx[k]=(26,24,10)(allocation:130,120,50) or(25,25,10)(allocation:125,125,50). Both options stay very close to the original demands.
二、问题所属的背包变体归类
This problem falls into the category of integer resource allocation with deviation minimization. If we're talking specifically about knapsack variants, it's a type of:
- Inverse Knapsack Problem: Traditional knapsack problems involve selecting items to maximize value without exceeding a weight limit. The inverse knapsack flips this: we have a fixed total resource (your
N), and we need to split it across multiple "bins" (your demand groups) while staying as close as possible to target amounts, with constraints on each bin's size (must be a multiple ofp).
It can also be described as a targeted integer partitioning problem, but "inverse knapsack" is the most accurate knapsack-specific label. Alternatively, it's a subproblem of integer linear programming (ILP) focused on resource allocation, but when framed within knapsack terminology, inverse knapsack is the right fit.
内容的提问来源于stack exchange,提问作者qoheleth

