# 数金字塔

17

``````5 1 2 6
5 2 6
5 6
6
``````

## 输入项

• 无重复的一个或多个整数的列表。（例如`1 5 1 6`无效。）

## 输出量

• 正整数列表。`i`列表的第th个元素是`i`金字塔中第th个输入数字的出现次数。

## 例子

``````-5 => 1

8 4 => 2 1

5 9 7 => 1 4 1

1 2 3 9 8 6 7 => 1 2 3 16 3 1 2

6 4 2 1 3 5 => 6 4 2 1 3 5

5 2 9 1 6 0 => 2 1 12 1 4 1

120 5 -60 9 12 1 3 0 1200 => 8 2 1 3 16 1 4 1 9

68 61 92 58 19 84 75 71 46 69 25 56 78 10 89 => 2 1 39 2 1 27 6 5 1 6 1 2 14 1 12
``````

Optimizer

4

# Pyth，19个 17字节

``````m/smmeSb.:QhkUQdQ
``````

### 说明

``````m/smmeSb.:QhkUQdQ    implicit: Q = input()
m         UQ         map each k in [0, 1, 2, ..., len(Q)-1] to:
.:Qhk              all subsets of Q of length (k + 1)
meSb                   mapped to their maximum
s                     join these lists together
m               Q    map each d of Q to:
/             d        its count in the computed list
``````

``````[[[5], [1], [2], [6]], [[5, 1], [1, 2], [2, 6]], [[5, 1, 2], [1, 2, 6]], [[5, 1, 2, 6]]]
``````

`mmeSk.:QhdUQ`给我它们的每个最大值（正好对应于金字塔中的行）：

``````[[5, 1, 2, 6], [5, 2, 6], [5, 6], [6]]
``````

# Pyth，23 22字节

``````|u&aYGmeSd.:G2QQm/sYdQ
``````

### 说明

`meSd.:G2`将每对映射`[(G[0], G[1]), (G[1], G[2]), ...]`到最大元素。

`Y`是一个空列表，因此`aYG`追加`G``Y`

`u...QQ`重复应用这两个功能（`len(Q)`时间），从每次运行开始`G = Q``G`在每次运行后进行更新。

`m/sYdQ`将输入列表的每个元素映射到它们在展平`Y`列表中的计数。

Optimizer

13

# Python，81岁

``````def f(L):
if L:i=L.index(max(L));L=f(L[:i])+[~i*(i-len(L))]+f(L[i+1:])
return L``````

``````* * * M * *
* * M M *
* M M M
M M M
M M
M
``````

``````def f(L):
[] -> []
A+[max(L)]+B -> f(A)+[(len(A)+1)*(len(B)+1)]+f(B)
``````

Martin Ender 2015年

6

# CJam，23 22字节

``````{]_,{)W\$ew::e>~}%fe=~}
``````

``````5 1 2 6 {]_,{)W\$ew::e>~}%fe=~}~
``````

``````3 1 2 4
``````

``````]_                     e# Wrap the input numbers on stack in an array and take a copy
,{          }%       e# Take length of the copy and run the loop from 0 to length - 1
)W\$                e# Increment the iterating index and copy the parsed input array
ew              e# Get overlapping slices of iterating index + 1 size
::e>          e# Get maximum from each slice
~         e# Unwrap so that there can be finally only 1 level array
fe=    e# For each of the original array, get the occurrence in this
e# final array created by the { ... }%
~   e# Unwrap the count array and leave it on stack
``````

3

# Mathematica，72个字节

``````Last/@Tally[Join@@NestList[MapThread[Max,{Most@#,Rest@#}]&,#,Length@#]]&
``````

3

# Python，81岁

``lambda L:[sum(x==max(L[i:j])for j in range(len(L)+1)for i in range(j))for x in L]``

1

# 点，56 +1 = 57字节

``````l:gr:0*,#gg:0*g+1WrFir:{c:r@[a--a]c@(\$<l@c)}M1,#r++(gi)g
``````

``````l:g                              l = input from cmdline args
r:0*,#g                          r = current row as a list of indices into l
g:0*g+1                          Repurpose g to store the frequencies
Wr                               Loop until r becomes empty
Fir:{c:r@[a--a]c@(\$<l@c)}M1,#r  Redefine r (see below) and loop over each i in it
++(gi)                         Increment g[i]
g                                Output g
``````

`r`通过工作的每次重新定义如下：

``````{c:r@[a--a]c@(\$<l@c)}M1,#r
{                   }M1,#r       Map this function to each a from 1 to len(r) - 1:
c:r@[a--a]                      c is a two-item list containing r[a] and r[a-1]
l@c              The values of l at the indices contained in c
\$<                 Fold/less-than: true iff l[c[0]] < l[c[1]]
c@(     )             Return c[0] if the former is greater, c[1] otherwise
``````

1

# APL（24）

``````{+/⍵∘.={⍵≡⍬:⍵⋄⍵,∇2⌈/⍵}⍵}
``````

``````      {+/⍵∘.={⍵≡⍬:⍵⋄⍵,∇2⌈/⍵}⍵}68 61 92 58 19 84 75 71 46 69 25 56 78 10 89
2 1 39 2 1 27 6 5 1 6 1 2 14 1 12
``````

• `{`...`}⍵`：对apply应用以下功能：
• `⍵≡⍬:⍵`：如果⍵为空，则返回⍵
• `2⌈/⍵`：生成下一个列表
• `⍵,∇`：返回⍵，然后是将此函数应用于下一个列表的结果
• `⍵∘.=`：将⍵中的每个元素与函数结果中的每个元素进行比较
• `+/`：对行求和（表示⍵中的元素）

1

``````l=length
f x=[l[b|b<-concat\$take(l x)\$iterate(zipWith max=<<tail)x,a==b]|a<-x]
``````

``````zipWith max=<<tail   -- apply 'max' on neighbor elements of a list
iterate (...) x      -- repeatedly apply the above max-thing on the
-- input list and build a list of the intermediate
-- results
take (l x) ...       -- take the first n elements of the above list
-- where n is the length of the input list
concat               -- concatenate into a single list. Now we have
-- all elements of the pyramid in a single list.
[ [b|b<-...,a==b] | a<-x]
-- for all elements 'a' of the input list make a
-- list of 'b's from the pyramid-list where a==b.
l                   -- take the length of each of these lists
``````

1

# JavaScript，109个字节

``````f=s=>{t=[];for(i=-1;s.length>++i;){j=k=i;l=r=1;for(;s[--j]<s[i];l++);for(;s[++k]<s[i];r++);t[i]=l*r}return t}
``````

i的出现次数=（小于其左侧i的连续数字的数量+ 1）*（小于小于其右侧i的连续数字的数量+ 1）

1

## MATLAB：（266羽）

• 代码的更正需要更多的字节，以后我将很难减少它。
``````v=input('');h=numel(v);for i=1:h,f=(v(i)>v(1));l=(v(i)>v(h));for j=h-1:-1:i+1,l=(v(i)>v(j))*(1+l);end,if(i>1),l=l+(v(i)>v(i-1))*l;end;for j=2:i-1,f=(v(i)>v(j))*(1+f);end,if(i<h),f=f+(v(i)>v(i+1))*f;end;s=f+l+1;if(i<h&&i>1),s=s-((v(i)>v(i+1))*(v(i)>v(i-1)));end;s
end
``````

• 例：

[2 4 7 11 3]

## 输出值

``````s =

1

s =

2

s =

3

s =

8

s =

1
``````

## 说明：

g =（a> b）+（a> b）（a> c）+（a> b）（a> c）*（a> d）=（a> b）（1+（a> c）（ 1+（a> c））））

h =（b> a）+（b> c）+（b> a）（b> c）+（b> c）（b> d）+（b> a）（b> c）（b> d ）= ...“简化”

i =（c> b）+（c> d）+（c> b）（c> d）+（c> b）（c> a）+（c> d）（c> b）（c> a ）= ..

j =（d> c）+（d> c）（d> b）+（d> c）（d> b）*（d> a）= ...

0

# J（49）

``````[:+/~.="1 0[:;([:i.#)<@:(4 :'(}:>.}.)^:x y')"0 1]
``````