ความเพลิดเพลินสูงสุด


17

คุณได้รับกระเป๋า Skittles ทุกคนรู้ว่าในการที่จะชื่นชมรสชาติที่แตกต่างกันมากที่สุดคุณต้องหมุนไปมาระหว่างรสชาติต่างๆ

ข้อมูลเบื้องต้น:

  1. คุณสามารถกินครั้งละ 1 skittle เท่านั้น
  2. คำสั่งที่คุณกิน skittles ของคุณจะต้องเป็นระยะ
  3. แต่ละช่วงเวลาไม่สามารถมีรสชาติที่เจาะจงมากกว่าหนึ่งครั้ง
  4. กระเป๋าของคุณมี skittles มากมาย คุณไม่สามารถกินของที่มีรสชาติเปราะบางได้มากกว่าที่ปรากฏในกระเป๋าของคุณ
  5. คุณต้องการกิน skittles มากเท่าที่คุณสามารถ (อาจไม่เป็นไปได้เสมอ)

ตัวอย่าง:

ให้บอกว่าคุณเริ่มต้นด้วย 3 Red, 2 Blue และ 3 Green skittles:

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)

Input / Output

  • คุณผ่านรายการจำนวนเต็มบวกที่ไม่ว่างเปล่าสำหรับการนับจำนวนสี (ตัวอย่างข้างต้นจะเป็น[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

นี่คือเพื่อให้การแก้ปัญหาของคุณสั้นที่สุดในภาษาที่คุณชื่นชอบ!


1
อาจเชื่อมโยงกับสิ่งนี้
Jonathan Allan

2
@JanathanAllan และนั่นคือเหตุผลที่ฉันต้องการคอมพิวเตอร์เพื่อให้แน่ใจว่าฉันมีความสุขกับการลื่นไถล :)
Nathan Merrill

คำตอบ:


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 เป็นอย่างไร
Jonathan Allan

@JonathanAllan นั่นคือวิธี JS : ค่าเริ่มต้นของการลด () ตั้งอยู่หลังการโทรกลับ การวางm=0ที่นี่เป็นการเหนี่ยวนำกอล์ฟเพราะฉันไม่สนใจค่าเริ่มต้นของลูปนี้ (มันจะถูกเขียนทับ) การเริ่มต้นmมีความสะดวก
Arnauld

ฉันเห็นว่ามันเหมือนกับการเรียกใช้ฟังก์ชันมากกว่าการวนซ้ำ (เช่นฟังก์ชันการลดของ Python มีค่าเริ่มต้นเป็นตัวเลือก)
Jonathan Allan

@JanathanAllan ใช่แน่นอน [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

ลองออนไลน์!

อย่างไร?

ทำซ้ำคำนำหน้าของดัชนีที่เรียงลำดับตามค่าจากมากไปน้อยทุกครั้งที่ทำได้โดยใช้ถุงที่ได้รับจากนั้นจึงเอา skittle หรือ skittles สุดท้ายออกจากแต่ละสิ่งเหล่านี้ตามความจำเป็นเพื่อให้สามารถทำได้และส่งกลับด้วย skittles มากที่สุด .

จำนวนที่ควรลบออกจากการทำซ้ำเป็นระยะ ๆ หนึ่งครั้งเป็นเพียงตัวเลขที่มีจำนวนขั้นต่ำในส่วนนำหน้านั้น

ỤṚ;\Ṛẋ"‘Ṣ$ḣ"Ç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 skittles หนึ่งสามารถจัดเรียงไว้ในตารางเรียงลำดับตามสีและจำนวน:

r g
r g b
r g b

หากมีใครพยายามที่จะกินอย่างแน่นอนฉัน skittles อย่างน้อยหนึ่งสามารถกิน i * c skittles ทั้งหมดโดยที่ c คือจำนวน skittles ในคอลัมน์ r-th เช่นสำหรับ i = 2 อย่างน้อยสามารถกิน 6 skittles

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 ไบต์ขอบคุณ Dead Possum


คุณสามารถเปลี่ยนไปsum(1for e in b if e[0]>c) sum(c<e[0]for e in b)มันจะแปลง True เป็น 1 โดยนัยและช่วยให้คุณประหยัด 5 ไบต์
Dead Possum
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.