按混排排序


18

块随机排序

块混洗排序是排序的列表的(而人工的)方法。举例说明,其工作方式如下。

[6, 1, 0, 3, 2, 4, -2, -1]
                            Break list into contiguous blocks
[6][1, 0][3, 2, 4][-2, -1]
                            Sort each block
[6][0, 1][2, 3, 4][-2, -1]
                            Sort blocks lexicographically
[-2, -1][0, 1][2, 3, 4][6]
                            Concatenate
[-2, -1, 0, 1, 2, 3, 4, 6]

划分为连续块的分区可以任意选择。但是,并不是所有的块选择都会在最后产生一个排序列表:

[6, 1, 0, 3, 2, 4, -2, -1]
[6, 1, 0][3, 2, 4][-2, -1]
[0, 1, 6][2, 3, 4][-2, -1]
[-2, -1][0, 1, 6][2, 3, 4]
[-2, -1, 0, 1, 6, 2, 3, 4]

如果所有块的长度均为1,或者只有一个块,那么结果当然会被排序。但是这些都是极端的情况。在此挑战中,您的任务是在块数和最大块长之间找到平衡。

任务

您的输入是整数L的非空列表,采用任何合理格式。您的输出应是最小的整数Ñ使得大号可以是块混洗排序,使得块的数目,并且每个块的长度是至多Ñ

每种语言的最低字节数为准。适用标准规则。

测试用例

[5] -> 1
[1,2] -> 2
[0,2,1,-1] -> 3
[-1,0,2,1] -> 2
[9,3,8,2,7] -> 4
[9,2,8,3,7] -> 3
[5,9,3,7,2,4,8] -> 7
[-1,-2,1,2,-1,-2,7] -> 4
[6,1,0,3,2,4,-2,-1] -> 4
[12,5,6,-6,-1,0,2,3] -> 3
[1,0,1,0,1,0,1,0,1,0] -> 6
[1,2,1,3,1,2,3,2,4,3] -> 5
[7,7,7,7,8,9,7,7,7,7] -> 4

Answers:


5

Brachylog23 22 20 19字节

感谢Zgarb,H.PWiz和Fatalize节省了一些字节。

~cᶠ{oᵐoc≤₁&≡ᵃlᵐ⌉}ˢ⌋

在线尝试!

我敢肯定,这里还有更多的高尔夫...

说明

~cᶠ      Find all lists that concatenate into the input, i.e. all partitions
         of the input.
{        Discard all partitions for which this predicate fails, and replace
         the rest with the output of this predicate.
  oᵐ       Sort each sublist of the partition.
  o        Sort the entire list.
  c≤₁      And require concatenation of the result to be sorted.
  &        Then:
  ≡ᵃ       Append the partition to itself.
  lᵐ       Map "length" over this list, i.e. we get the length of each block, as
           well as the length of the partition itself.
  ⌉        Take the maximum.
}ˢ
⌋        Take the minimum of all those maxima.

3

果冻,17个字节

Ṣ€ṢF
ŒṖÇÐṂ+Z$€L€Ṃ

在线尝试!

替代版本,15字节,日期挑战

在最新版本中,Ɗ将三个链接组合成一个单子链。这样可以进行以下高尔夫运动。

ŒṖṢ€ṢFƊÐṂ+ZLƊ€Ṃ

在线尝试!

怎么运行的

Ṣ€ṢF          Helper link. Argument: P (partitioned array)

Ṣ€            Sort each chunk.
  Ṣ           Sort the sorted chunks.
   F          Flatten.


ŒṖÇÐṂ+Z$€L€Ṃ  Main link. Argument: A (array)

ŒṖ            Generate all partitions of A.
  ÇÐṂ         Keep those for which the helper link returns the minimal array, i.e.,
              those that return sorted(A).
     +Z$€     Add each partition to its transpose.
              Due to how Jelly vectorizes, the length of the sum is the maximum of
              the length of the operands, and the length of the transpose is the
              length of the array's largest column.
         L€   Take the length of each sum.
           Ṃ  Take the minimum.

2

Stax28 26 25 24 23 字节CP437

é%(>ù│ê²☻û◙T╠►╜◘íaæAtI╥

在线运行和调试!

@recursive可节省3个字节。

Stax在这里有点冗长。默认情况下,需要两个字节来对数组进行排序,两个字节来获取数组的最大/最小值,两个字节来展平数组。无论如何,我仍在发布解决方案,并希望那里可以提供有关如何改进它的有用建议

说明

使用解压后的版本进行解释。

%cFxs|!F{{omo:f:^!C_Mch\%|m
%cFxs|!F                        Do for all partitions, grouped by number of sub-arrays
                                    Grouping by number of sub-arrays in this problem does not help but it's the default                    
        {{om{o                  Sort inside block then sort blocks lexicographically
              :f:^              The result when flattened is sorted
                  !C            Skip the rest of the loop if the last line is false
                    _|<         Take the current partition, pad to the longest

                       h        Take the first element, whose length is now the maximum of all sub-arrays in the original partition
                        \       Zip with the current partition, the shorter one is repeated
                         %      Number of elements
                                Which is the maximum of all sub-array sizes and the number of sub-arrays in the current partition  
                          |m    Take the minimum among all choices of partitions

给出了25
递归

1
对于stax来说,这是一种令人失望的性能,但是我将继续寻找节省的方法。
递归


真是太神奇了。

谢谢。我发现在将“ https://”替换为“ http://”之后,超链接恰好使用了最大注释大小,这很有趣。
递归

2

Brachylog,17个字节

~c₎{oᵐoc≤₁&lᵐ⌉≤}ᵈ

在线尝试!

说明

这是一个自我解答;我在设计Brachylog时遇到的挑战,并发现~c₎{…}ᵈ了一个有趣的结构。

内置c函数连接列表列表。如果给其下标N,则要求列表的数量为N。该符号修改了内置符号,以将一对作为输入并使用其正确的元素作为下标。从而c₎取一对[L,N],要求中的列表数LN,并返回的串联L。反向运行时,~c₎获取一个列表L并返回pair [P,N],其中PLN块的分区。它们以的递增顺序枚举N

谓词将普通谓词转换为检查对中两个元素之间的关系的谓词。更明确地说,{…}ᵈ取一对[A,B],检查A{…}B保持并输出B。我使用它来验证P可以进行块排序,并且最多仅包含长度列表N

~c₎{oᵐoc≤₁&lᵐ⌉≤}ᵈ  Input is a list, say L = [9,2,8,3,7].
~c₎                Guess the pair [P,N]: [[[9],[2],[8,3,7]],3]
   {           }ᵈ  Verify this predicate on P and N and return N:
    oᵐ              Sort each list in P: [[9],[2],[3,7,8]]
      o             Sort lexicographically: [[2],[3,7,8],[9]]
       c            Concatenate: [2,3,7,8,9]
        ≤₁          This list is nondecreasing: true.
          &lᵐ       Length of each list in P: [1,1,3]
             ⌉      Maximum: 3
              ≤     This is at most N: true.

请注意,其中P可能包含空列表。在块的最大长度大于块数的情况下,这也确保了正确性。



1

红宝石,158字节

f=->l,i=[],s=l.size,m=s{k=*l;i.sum==s&&i.map{|z|k.shift(z).sort}.sort.flatten==l.sort&&[m,[i.size,*i].max].min||i.sum<s&&(1..s).map{|z|f[l,i+[z],s,m]}.min||m}

在线尝试!


1

Pyth 24 23  20字节

hSmeSlMs]Bd.msSSMb./

测试套件。

怎么运行的

hSmeSlMs]Bd.msSSMb./ – Full program. Hereby, Q represents the input.
                  ./ – All possible partitions of Q.
           .m        – Take the partitions which yield a minimal (i.e. sorted) list over:
             sSSMb   – Sorting function. Uses the variable b.
               SMb   – Sort each list in each partition b.
              S      – Sort the partition b.
             s       – And flatten (by 1 level).
  meSlMs]Bd          – Map. Uses a function whose variable is d.
        ]Bd          – Pair d with its wrapping in a singleton list. Returns [d, [d]].
       s             – Flatten (by 1 level). Returns [*d, d], where "*" is splatting.
     lM              – Take the length of each element.
   eS                – Retrieve the maximal length.
hS                   – Return the minimum element of the list of maximal lengths.

0

APL(Dyalog Classic)71 67字节

{⌊/(⌈/≢,≢¨)¨T/⍨{S[⍋S]≡∊⍵[⍋↑⍵]}¨T←{⍵[⍋⍵]}¨¨⊂∘S¨(-≢S)↑¨2⊥⍣¯1¨⍳2*≢S←⍵}

⎕IO 一定是 0

在线尝试!

怎么样?

  • ⌊/- 找到...的最小值
  • (⌈/≢,≢¨)- ...的最大长度和元件的数目的...
  • ¨- 每个元素的......
  • T/⍨- ...的元素...
  • {S[⍋S]≡∊⍵[⍋↑⍵]}¨- ...扁平的时候,排序...
  • T←{⍵[⍋⍵]}¨¨- ...的元素排序元素...
  • ⊂∘S¨(-≢S)↑¨2⊥⍣¯1¨⍳2*≢S←⍵- ...参数的分区(一些垃圾一起)
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.