测量一堆原木


16

介绍

这是长度为5的对数:

#####

我想将这些原木堆在一起。我的操作方式是将新的日志从右侧滑动到最上面的日志,并在它们的左端或右端对齐时停止滑动(不要问为什么)。如果新日志更长,它将一直滑动到最上面的日志的左端:

########  <-
#####

如果更短,则仅滑动直到其右端对齐:

  ######  <-
########
#####

当我将更多的原木滑入桩中时,它们的位置由当前最上面的原木确定:

           ##
       ######
       ###
      ####
      ##
  ######
########
#####

这看起来在物理上是不可能的,但让我们假装它可行。

任务

您的输入应该是一个非空的正整数列表,代表我的日志长度。最左边的数字是我放到堆中的第一个原木,因此它最终在底部。在上面的示例中,输入为[5,8,6,2,4,3,6,2]。对于所得桩的每一列,您的输出应为越过该列的原木数量。在以上示例中,正确的输出为[2,2,3,3,3,2,4,6,3,3,1,2,2]

规则和计分

输入和输出可以采用任何合理的格式。输出只能包含正整数,即它不得包含前导或尾随0。普通的代码高尔夫球规则适用:您可以编写完整的程序或函数,最低字节数获胜,并且禁止标准漏洞

测试用例

[1] -> [1]
[4] -> [1,1,1,1]
[3,2] -> [1,2,2]
[2,3] -> [2,2,1]
[2,2,2] -> [3,3]
[2,3,2] -> [2,3,2]
[3,2,3] -> [1,3,3,1]
[1,3,2,2,1,3,1] -> [2,3,5,1,2]
[4,3,4,2,4,3,4,2] -> [1,3,3,5,5,3,4,2]
[5,8,6,2,4,3,6,2] -> [2,2,3,3,3,2,4,6,3,3,1,2,2]
[5,10,15,1,1,1,1,1,2] -> [3,3,3,3,3,2,2,2,2,2,1,1,1,1,7,1]
[13,12,2,10,14,12] -> [1,2,2,2,2,2,2,2,2,2,2,5,5,3,3,3,3,3,3,3,3,2,2,2,2]
[12,14,3,6,13,1,1] -> [2,2,2,2,2,2,2,2,2,2,2,5,4,4,2,2,2,1,1,1,1,1,1,3]
[7,5,12,5,1,10,14,5] -> [1,1,3,3,3,3,3,1,1,2,2,2,2,5,2,2,2,2,2,2,2,2,3,2,2,2,2]
[14,5,1,3,12,6,2,2,1,7,9,15] -> [1,1,1,1,1,1,1,1,1,2,2,2,2,5,2,2,1,1,1,2,2,2,2,4,8,3,3,3,3,3,3,2,2,1,1,1,1,1,1]

2
我们可以轻松地“假装”它起作用,只需将它们全部放在地面上,而不是将它们堆叠在空中(将它们彼此相邻滑动)。
乔纳森·艾伦

1
最后一个测试用例看起来像挪威!
Stewie Griffin

Answers:


7

果冻 18  16 字节

-2字节由英里帮助提示

也许有一种更快的方式使用数学而不是像这样的构造?

IN0;»0+\0ẋ;"1ẋ$S

在线尝试!或查看测试套件

怎么样?

IN0;»0+\0ẋ;"1ẋ$S - Link: list of positive integers, logLengths  e.g. [4, 3, 3, 1, 4, 3]
I                - incremental differences                            [-1, 0,-2, 3,-1]
 N               - negate (vectorises)                                [ 1, 0, 2,-3, 1]
  0;             - zero concatenated with that                      [0, 1, 0, 2,-3, 1]
    »0           - maximum (vectorises) of that and zero            [0, 1, 0, 2, 0, 1]
      +\         - cumulative reduce with addition                  [0, 1, 1, 3, 3, 4]
        0ẋ       - zero repeated (vectorises)    [[],[0],[0],[0,0,0],[0,0,0],[0,0,0,0]]
              $  - last two links as a monad (right is implicitly logLengths):
            1ẋ   -   one repeated     [[1,1,1,1],[1,1,1],[1,1,1],[1],[1,1,1,1],[1,1,1]]
           "     - zip with:
          ;      -   concatenation
              [[1,1,1,1],[0,1,1,1],[0,1,1,1],[0,0,0,1],[0,0,0,1,1,1,1],[0,0,0,0,1,1,1]]
              ... this is an upside-down version of the logs like those in the OP
                  with 0 for spaces and 1 for # with any right-hand-side spaces missing.
               S - sum                                           [1, 3, 3, 5, 2, 2, 2]

如果我们结合使用我们的解决方案,我们可以得到17个字节:IN»0+\0;;"x@€0,1S
英里

7

果冻19 13字节

IN0»0;+\+"RṬS

在线尝试!

感谢@Jonathan Allan,节省了2个字节。

说明

IN0»0;+\+"RṬS  Input: array A
I              Increments
 N             Negate
  0»           Max with 0
    0;         Prepend 0
      +\       Cumulative sum
        +"     Vectorized add with
          R    Range, vectorizes over each integer
           Ṭ   Create a list with 1's at the specified indices
            S  Sum


3

外壳,16字节

Fż+Ṡzo`Ṙ↔ḋ2eo∫Ẋ<

在线尝试!

说明

              Ẋ    For all adjacent pairs, x y
               <   return max(0,x-y)
            o∫     Cumulative sum, with an extra 0 at the start
   Ṡz              Zip the input list and ^ with ...
           e         Make a two element list
        ↔ḋ2          The list [0,1]
     o`Ṙ             Repeat each in ^ by ^^
Fż+               Sum the columns



0

Kotlin 1.1, 113个 103字节

{var l=0
var r=0
it.flatMap{l=maxOf(l,r-it+1)
r=l+it-1
(l..r).toList()}.groupBy{it}.map{it.value.size}}

美化

{
    // Current row leftmost value
    var l = 0
    // Current row rightmost value
    var r = 0
    // For each log
    it.flatMap {
        // Work out the new leftmost point
        l = maxOf(
                l,
                r - it+1)
        // Use it to work out the new rightmost point
        r = l + it-1
        // Record the used columns
        (l..r).toList()}
            // Group the column numbers together
            .groupBy { it }
            // Count the amount of times each column is used
            // Put the results into a list
            .map { it.value.size }
}

测试

var z:(List<Int>)->List<Int> =
{var l=0
var r=0
it.flatMap{l=maxOf(l,r-it+1)
r=l+it-1
(l..r).toList()}.groupBy{it}.map{it.value.size}}

fun main(args: Array<String>) {
    println(z(listOf(5, 8, 6, 2, 4, 3, 6, 2)))
    println(listOf(2,2,3,3,3,2,4,6,3,3,1,2,2))
}
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.