CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2006
    Posts
    41

    Optimal distribution by blocks

    I have the following task. I have an array of non-negative numbers, let us name it A. For instance: {30, 3, 2892}. In fact, each of the numbers is count of some objects (it does not matter – which objects). I have to distribute the objects by blocks with size X (this is desired value). If A(i) is divisible by X, then we suppose A(i) requires number of blocks equal to A(i)/X; otherwise - (A(i)/X + 1). Of course, when A(i) < X, then A(i) requires one block.

    Example: if for our array {30, 3, 2892} X is 10, then:
    - A(0) requires 3 blocks;
    - A(1) requires 1 block;
    - A(2) requires 290 blocks;

    X must satisfy the following conditions:

    a) Minimal possible value of X is MinSize (given value)
    b) Total number of blocks for ALL items of array A for given X should not be more than MaxCount (given value)
    c) The total number of blocks mentioned in (b) should be largest for given X

    Thus we have optimization task: lesser X gives larger total number of blocks; and in the same time the total number of blocks should not be more than MaxCount.

    How should I solve the task? Is there analytical (non- iterative) algorithm?

  2. #2
    Join Date
    Jan 2008
    Posts
    12

    Re: Optimal distribution by blocks

    How about using binary searching? Firstly, you choose the minimum and the maximum possible of X, called low and high, for instance. And then, mid = (low + high) / 2, calculating the total blocks of all items, compare it with MaxCount, and then adjust low = mid or high = mid depending on the result of the comparison.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured