最大のスキトルの楽しみ


17

スキトルズのバッグが渡されました。さまざまなフレーバーを最大限に活用するには、フレーバー間をローテーションする必要があることは誰もが知っています。

基本:

  1. 一度に食べることができるスキトルは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
@ジョナサンアラン、そしてそれは私のスキトルの楽しみを確保するためにコンピュータが必要な理由です:)
ネイサンメリル

回答:


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 ]です。です。

この式は次のように解釈できます。Skttleタイプごとに、使用可能なタイプの数に1を足した数以下を選択できます。

 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そこに初期化すると便利です。
アーナウルド

ああ、ループよりも関数呼び出しのようです(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ます。
アルノー

2

ゼリー、22 バイト

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

1ベースのインデックス。

オンラインでお試しください!

どうやって?

インデックスの各接頭辞を、値で降順でソートし、スキトルの特定のバッグで達成できるよりも1回多く繰り返し、必要に応じてこれらのそれぞれから最終スキトルを削除して、それらを達成可能にし、最もスキトルのあるスキトルを返します。

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)-~i2バイト節約します。

編集:デッドポッサムのおかげで-5バイト


を変えることができます sum(1for e in b if e[0]>c)sum(c<e[0]for e in b)。これは、暗黙的に1に真変換し、あなたに5つのバイトを救う
デッドポッサム
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.