最大的吃喝玩乐


17

您已经收到了一袋吃喝玩乐的东西。大家都知道,为了最欣赏不同的口味,您需要在不同的口味之间旋转。

基本:

  1. 一次只能吃一吃喝玩乐
  2. 吃吃喝玩乐的顺序必须定期。
  3. 每个时期最多只能包含一种风味。
  4. 你的包里只有这么多吃喝玩乐。吃的吃喝玩乐的味道不能超过包中的味道。
  5. 您想吃尽可能多的吃喝玩乐(可能并不总是可能的)

例子:

假设您以3个红色,2个蓝色和3个绿色彩虹糖开始:

R B G R B G R G       Invalid:  The last R must be followed by a B, not a G
R B G R B G R         Valid, but sub-optimal
R R R                 Valid, but sub-optimal
R G B R G B R G       Valid and optimal
G R B G R B G R       Also valid and optimal (there are multiple good solutions)

输入输出

  • 您将获得一个非空的正整数列表,用于颜色计数。(以上示例为[3,2,3])。
  • 您需要返回一个包含有效和最佳排序的列表。
  • 您将使用输入列表中的索引,而不是使用颜色。(上面的最后一个示例输出是[2,0,1,2,0,1,2,0])。
  • 您的输出可能是0索引或1索引。我的示例将以0索引

测试用例

1                          0
4                          0 0 0 0
4 1                        0 0 0 0
3 1                        0 1 0                   or  0 0 0
5 2 2                      0 1 2 0 1 2 0
2 3 5                      2 1 0 2 1 0 2 1         or  1 2 0 1 2 0 1 2
2 4 5                      2 1 2 1 2 1 2 1 2
3 4 5                      2 1 0 2 1 0 2 1 0 2 1   or  1 2 0 1 2 0 1 2 0 1 2
1 1 1 1 1 6                5 0 1 2 3 4 5           (lots of other solutions)
1 1 1 1 1 8                5 5 5 5 5 5 5 5
2 4 6 8                    3 2 1 3 2 1 3 2 1 3 2 1 3 2

这是一个,因此请以您喜欢的语言使您的解决方案尽可能短!



2
@JonathanAllan,这就是为什么我需要一台计算机来确保吃喝玩乐的原因:)
Nathan Merrill

Answers:


4

JavaScript的(ES6),177个 175字节

a=>a.map((n,i)=>[n,l=i]).sort((a,b)=>a[0]-b[0]).reduce((P,x,i,a)=>(v=a.reduce((p,c,j)=>j<i?p:p+Math.min(c[0],x[0]+1),0))>m?[...Array(m=v)].map((_,k)=>a[l-k%(l+1-i)][1]):P,m=0)

格式化和评论

a => a                              // given an array a:
.map((n, i) => [n, l = i])          // map it to [value, index] arrays / set l = length - 1
.sort((a, b) => a[0] - b[0])        // sort it by values in ascending order
.reduce((P, x, i, a) =>             // for each reference entry x at position i:
  (v = a.reduce((p, c, j) =>        //   for each entry c at position j:
    j < i ?                         //     if c is before x:
      p                             //       keep the previous sum (which is 0)
    :                               //     else:
      p + Math.min(c[0], x[0] + 1), //       add minimum(value[j], value[i] + 1)
    0                               //   initialize the sum at 0
  )) > m ?                          //   if the new sum v is better than our current best m:
    [...Array(m = v)].map((_, k) => //     update m to v and update the result to an array
      a[l - k % (l + 1 - i)][1]     //     of length m filled with indices picked repeatedly
    )                               //     between i and l
  :                                 //   else:
    P,                              //     keep the previous result
  m = 0                             // start with best score m = 0
)                                   // the final result is returned by the outer reduce()

二手公式

下表显示了公式F(i, j) = minimum(value[j], value[i] + 1)的工作方式,此处带有i = 0和输入[ 5, 2, 2 ]

这个公式可以解释为:对于每种Skittle类型,我们选择的数量不能超过最少可用类型的数量加一。

 j | Sorted    | value[j] | F(0, j) | Selected        | Output
   | input     |          |         | Skittles        | (starting from bottom left)
---+-----------+----------+---------+-----------------+-----------------------------
 0 | 2 2       |     2    |    2    | [2] [2]         | \
 1 | 1 1       |     2    |    2    | [1] [1]         |  > 0 1 2 0 1 2 0
 2 | 0 0 0 0 0 |     5    |    3    | [0] [0] [0] 0 0 | /

测试用例


和(0)的归约初始化m是在高尔夫引起的“循环”结束时发生,还是只是JS如此?
乔纳森·艾伦

@JonathanAllan 这就是JS方式:reduce()的初始值位于回调之后。把m=0这里打高尔夫球引起的,但是,因为我不关心这个循环的初始值(它会被覆盖反正)。初始化m很方便。
Arnauld

嗯,我看到它更像是函数调用而不是循环(例如Python的reduce函数具有可选的初始值)。
乔纳森·艾伦

@JonathanAllan是的,完全是。我认为[1,2,3].reduce((x, y) => x+y, 10)JS中的代码将reduce(lambda x,y: x+y, [1,2,3], 10)在Python中使用,都导致16
Arnauld

2

果冻,22 字节

ċЀṢN
ỤṚ;\Ṛẋ"‘Ṣ$ḣ"ÇLÞṪ

基于1的索引。

在线尝试!

怎么样?

重复按值排序的索引的每个前缀,降序比给定的九柱吃喝玩乐所能达到的时间多一倍,然后根据需要从其中的每一个中删除最后一个九柱吃喝玩乐,以使它们成为可实现的,并返回那些吃喝玩乐最多的人。

应该从一个额外的周期性重复中删除的数字就是该前缀上具有最小计数的数字。

ỤṚ;\Ṛẋ"‘Ṣ$ḣ"ÇLÞṪ - Main link                   e.g. [6,4,2,8]
Ụ                - grade up: sort indices by value  [3,2,1,4]
 Ṛ               - reverse                          [4,1,2,3]
   \             - cumulative reduce with
  ;              -     concatenation (get prefixes) [[4],[4,1],[4,1,2],[4,1,2,3]]
    Ṛ            - reverse                          [[4,1,2,3],[4,1,2],[4,1],[4]]
         $       - last two links as a monad
       ‘         -     increment                    [7,5,3,9]
        Ṣ        -     sort                         [3,5,7,9]
      "          - zip with
     ẋ           -     list repetition              [[4,1,2,3,4,1,2,3,4,1,2,3],[4,1,2,4,1,2,4,1,2,4,1,2,4,1,2],[4,1,4,1,4,1,4,1,4,1,4,1,4,1],[4,4,4,4,4,4,4,4,4]]
            Ç    - call last link (1) as a monad    [-1,-1,-1,-1]
          "      - zip with
           ḣ     - head list to (remove excess)     [[4,1,2,3,4,1,2,3,4,1,2],[4,1,2,4,1,2,4,1,2,4,1,2,4,1],[4,1,4,1,4,1,4,1,4,1,4,1,4],[4,4,4,4,4,4,4,4]]
              Þ  - sort by
             L   -     length                       [[4,4,4,4,4,4,4,4],[4,1,2,3,4,1,2,3,4,1,2],[4,1,4,1,4,1,4,1,4,1,4,1,4],[4,1,2,4,1,2,4,1,2,4,1,2,4,1]]
               Ṫ - tail                             [4,1,2,4,1,2,4,1,2,4,1,2,4,1]

ċЀṢN - Link 1: head amounts (negative of skittle excess of each N+1 repeated period)
   Ṣ  - sort                                        [2,4,6,8]
 Ѐ   - for each mapped over right argument
ċ     - count                                       [1,1,1,1]
    N - negate                                      [-1,-1,-1,-1]

1

Python3, 174 172 167字节

理念

假设有3个红色,2个蓝色和3个绿色吃喝玩乐,就可以将它们排列成网格,并按颜色和数量排序:

r g
r g b
r g b

如果一个人尝试吃掉i的吃喝玩乐,那么总共至少可以吃掉i * c吃喝玩乐,其中c是第r列中吃喝玩乐的数量,例如,对于i = 2,一个人至少可以吃掉6个吃喝玩乐。

r g
# # #
# # #

剩下要做的唯一一件事就是计算一个不完整的时期可以吃掉多少吃喝玩乐。

打高尔夫球

def f(a):
 r=range;f=m=0;s=r(len(a));b=sorted(zip(a,s))[::-1]
 for i in s:
  c=b[i][0];n=-~i*c+sum(c<e[0]for e in b)
  if n>m:f,m=i+1,n
 return[b[j%f][1]for j in r(m)]

已评论

def f(a):
    r = range;
    f = m = 0;                          - Some variables we need later on
    s = r(len(a));                      - Integers from 0 to (num_skittles - 1)
    b = sorted(zip(a,s))[::-1]          - Zip with s to remember initial order,
                                          then sort and reverse
    for i in s:
        c = b[i][0]
        n = (i+1)*c                     - If we attempt to eat i different skittles,
                                          we can surely eat (i+1)*c skittles.
          + sum(1 for e in b if e[0]>c) - The additional sum corresponds to an incomplete period.
        if n>m:                         - If a better way of eating skittles is found:
            f,m = i+1,n                 - update variables
    return [b[j%f][1] for j in r(m)]

在线尝试!

编辑:替换(i+1)使用-~i,以节省2个字节。

编辑:-5字节感谢死鼠


您可以更改sum(1for e in b if e[0]>c)sum(c<e[0]for e in b)。它将隐式将True转换为1,并节省5个字节
Dead Possum
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.