尽可能平均地分配金额 [英] Distributing an amount as evenly as possible

查看:113
本文介绍了尽可能平均地分配金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一定数量,例如300台该数量应尽可能均匀地分布在40个插槽上。如果每个插槽都相同将很容易-因此每个插槽将为7.5。但是,这些插槽的大小各不相同,因此我们无法在其中填充超过其大小所允许的数量,例如如果只有5个。我们无法填充的内容我们必须分配给其他对象。

We have have a certain amount e.g. 300 units. This amount should be as evenly as possible distributed over 40 "slots". It would be easy if each slot would be the same - so it would be 7,5 at each slot. However, the slots vary in size and we cannot "fill in" there more than its "size" allows for e.g. if its only 5. What we cannot "fill in" we have to distribute more over the other ones.

我有一些基本想法,但我离成为希望并有一种简单的方法可以解决此问题。
举个例子。在数组 a中,这些值代表插槽可以占用的最大值。 a [i]是第i个时隙的最大值。 b是我们必须分发的全部内容,例如300。

I have some basic ideas but I am far away from being an expeRt and hope there is an easy way to solve this. As an example how this could look like. In array "a" the values stand for the maxima the slots can take. a[i] is the maximum of the i-th slot. "b" is what we have to distribute overall e.g. 300.

 # developing slots and their "size"
 a <- rnorm(40,10,4)
 sum(a)

 # overall sum to distribute
 b <- 300 

也许可以按升序对值进行排序,然后可以通过double for循环使用它。 a [,2]成为已填入金额的列。

Maybe it is possible to sort the values in a increasing order and then one could use it by a double for loop. a[,2] becomes the column for the "filled in" amount.

 for i in 1:40
 {a[i,2] <- a[1,2]*40
 b <- a [1,2]*40}

 for i in 2:40
 {a[i,2] <- a[1,2]*39
 b <- a[1,2]*39}

etc.

我不确定如何将两个for循环放在一起,如果这总体上是一个合适的解决方案。
很高兴听到您的想法。谢谢!

I am not sure how I can put the both for loops together and if this is an adequate solution overall. Happy to hear your ideas. Thanks!

推荐答案

第一个版本,使用while循环:

First version, using a while loop:

optimal.fill <- function(a, b) {
  stopifnot(sum(a) >= b)

  d <- rep(0, length(a))
  while(b > 0) {
    has.room  <- a > 0
    num.slots <- sum(has.room)
    min.size  <- min(a[has.room])
    add.size  <- min(b / num.slots, min.size)
    d[has.room] <- d[has.room] + add.size
    a[has.room] <- a[has.room] - add.size
    b <- b - num.slots * add.size
  }
  return(d)
}

第二个版本很难理解,但我觉得更优雅:

This second version is a little harder to understand, but more elegant I feel:

optimal.fill <- function(a, b) {
  stopifnot(sum(a) >= b)

  slot.order   <- order(a)
  sorted.sizes <- a[slot.order]
  can.fill     <- sorted.sizes * rev(seq_along(a))
  full.slots   <- slot.order[which(cumsum(can.fill) <= b)]

  d <- rep(0, length(a))
  d[ full.slots] <- a[full.slots]
  d[!full.slots] <- (b - sum(a[full.slots])) /
                    (length(a) - length(full.slots))

  return(d)
}

这篇关于尽可能平均地分配金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆