海马鞋


30

当然,海马需要鞋子。但是,只有一条尾巴的海马只需要一只鞋子。不幸的是,这双鞋只成对出现。对于海马政府来说,资金紧张,因此他们需要购买尽可能少的货币对。每只海马的鞋子尺码为x,其中x为正整数。但是,如果需要,海马可以穿x-1x + 1的鞋子。

您的任务是输出海马政府必须购买以在其所有海马上穿鞋的最小对数。

您可以根据需要输入,也可以输入标准漏洞等。

因为这是,所以以字节为单位的最短代码获胜。

测试用例

2 4 6 6 8 14 ->        4
2 1 3 1 1 ->           3
4 1 4 9 1 8 9 1 8 4 -> 6
1 2 3 5 7 8 10 12 ->   4

可以通过对数组进行排序和遍历遍历来轻松完成此操作,但是我希望看到一些有创意的东西(这与实际评分没有关系,我只是想看看另一种方法会很有趣)
操纵时间:2017年

1
我不知道如何轻松地完成它……
Leaky Nun

5
@ bushdid911我想我无法解释果冻是如何工作的评论
漏修女

1
@CodyGray您可以拥有3码对,包括2和
4。– Zgarb

2
可能的标题编辑:Sea-horseshoes
CraigR8806 '17

Answers:


5

05AB1E,13个字节

使用注释中描述的方法OP。

{¥3‹J0¡€gÌ2÷O

在线尝试!

说明

{¥3‹J0¡€gÌ2÷O   Argument l
{               Sort l
 ¥              Push deltas
  3‹            Map to lower than 3 (1 for true, 0 for false)
    J0¡         Join and split on 0
       €g       Map to length
         Ì      Each + 2
          2÷    Integer division by 2
            O   Sum

8

外壳15 14字节

Γ0(→₀?tI↑<+3)O

使用贪婪算法:从左开始排序和配对。 在线尝试!

感谢Leo节省了1个字节。

说明

这是第一个使用Husk的答案Γ,该功能用于匹配列表的模式。在此用例中,如果a是一个值并且g是一个函数,则Γag对应于fHaskell片段定义的函数

f [] = a
f (x:xs) = g x xs

我定义基础情况下a = 0

g x xs = 1 + line0 (if head xs < x+3 then tail xs else xs)

这里line0指的是整行。在Husk代码中,xxs是lambda函数的隐式参数,并且line0。该列表在每次递归调用中都再次排序,但这在高尔夫挑战赛中并不重要。

Γ0(→₀?tI↑<+3)O
             O  Sort
Γ               and pattern match
 0              giving 0 for an empty list
  (         )   and applying this function to a non-empty list:
          +3     Add 3 to first argument (x),
         <       make a "test function" for being less than that,
        ↑        take values from second argument (xs) while they pass the test.
     ?           If that prefix is nonempty (next value can be paired),
      t          take tail of xs,
       I         otherwise take xs as is.
    ₀            Apply the main function (line0) to this list
   →             and add 1 for the singleton/pair we just processed.

所有这些使用自己的语言的人都让我想要创建自己的语言。首先,我想出一个名字:P
操纵


4

果冻20 18字节

ṢLµIḢ<3+2⁸ṫß‘µLỊ$?

在线尝试!

我的Python回答的前叉。


-4字节:IḢ<3+2⁸ṫß‘µLḊ?(基本上看不到任何理由这样做之前L,并且将返回[]如果长度为1或0,如果列表中,然后我可以移除µLµḊ?
埃里克Outgolfer

但是您什么地方都没整理...
Leaky Nun

现在我有点困惑tbf ...我认为您的意图与您的代码实际有所不同?如果我理解正确的话,您可能想在我的高尔夫前加一个。
暴民埃里克(Erik the Outgolfer)'17年

有点怪异的东西。[1、1、1、1、4、4、4、8、8、9、9]有效,但[4,1,4,9,1,8,9,1,8,4,1]不起作用t。
操纵

@ bushdid911他们都工作。你能示范一下吗?
Leaky Nun

4

Python 2,49个字节

f=lambda a:a>[a.sort()]and-~f(a[[3+a.pop(0)]>a:])

在线尝试!

基于Leaky Nun的递归解


Python 2,59个字节

p=c=0
for x in sorted(input()):c+=x>p;p=(x>p)*(x+2)
print c

在线尝试!

x按大小顺序遍历大小。记住p当前大小与上一个配对的上限。如果是这样(x>p),请重置阈值以0使下一个不能配对。如果不是,请增加输出计数c并将下一个阈值设置px+2

新的阈值p=(x>p)*(x+2)是expression肿的表情。我想找到一种缩短它的方法。


2

C#,111个 108 137 102字节

这永远不会赢,但是我还是想解决这个问题:

Array.Sort(a);var c=0;for(var i=0;i<a.Length;i++){c++;i+=a.Length-i>1&&a[i+1]-a[i]<3?1:0;}Console.WriteLine(c);

感谢@grabthefish的评论,我可以再咬几个字节:

Array.Sort(a);int c=0,i=0;for(;i<a.Length;i++){c++;i+=a.Length-i>1&&a[i+1]-a[i‌​]<3?1:0;}Console.Wri‌​teLine(c);

遵循PC&G特殊的C#规则:

class P{static void Main(){Array.Sort(a);int c=0,i=0;for(;i<a.Length;i++){c++;i+=a.Length-i>1&&a[i+1]-a[i]<3?1:0;}Console.WriteLine(c);}}

使用lambda函数:

a=>{System.Array.Sort(a);int c=0,i=0;for(;i<a.Length;c++)i+=a.Length-i>1&&a[i+1]-a[i]<3?2:1;return c;}

评论不作进一步讨论;此对话已转移至聊天
丹尼斯,

感谢您通过答案保持进度-与最终答案一样有趣。
Criggie'7

2

Perl,113个字节

say sub{for(1..$#_){$x{$i}++;$i++if$_[$_]-$_[$_-1]>2}$x{$i}++;$-+=$_/2+$_%2for values%x;$-}->(sort{$a<=>$b}@ARGV)

从命令行获取参数列表(如@ARGV),STDOUT默认情况下输出到。

在Seahorseville ...

一个邻居是相邻的鞋码的序列。排序后,每只海马都有可以共享相同鞋子尺码的直接邻居。邻居中可以有多个邻居,并且邻居的值相差不能超过两个:

例如,3 3 4 5 5 6是一个居民区2 4 6 6,并且1 2 3 5 7 8 10 12

例如1 1 1 4 5 6包含两个街区:1 1 14 5 6

算法基础

有两种类型的社区:

  • 大小均匀

    对于这些,n/2配对始终足够:

    例如,3 3 4 5 5 6需要三对3 34 5并且5 6

  • 奇数大小

    对于这些,ceil(n/2)配对始终足够:

    例如,12 13 13 14 15对于,和12 13,需要三对。13 1415

未经测试的代码来测试算法

sub pairs {
    @_ = sort { $a <=> $b } @_;
    my @hood;
    my $i = 0;
    for (1..$#_) {
        push @{$hood[$i]}, $_[$_-1];
        $i++ if $_[$_]-$_[$_-1]>2
    }
    push @{$hood[$i]}, $_[$#_];
    my $pairs;
    $pairs += int(@{$hood[$_]} / 2) + @{$hood[$_]} % 2 for 0..$#hood;
    return "$pairs : @{[map qq([@$_]), @hood]}\n";
}

样品结果

(附在附近 [ ]

4 : [2 4 6 6 8] [14]
3 : [1 1 1 2 3]
6 : [1 1 1] [4 4 4] [8 8 9 9]
4 : [1 2 3 5 7 8 10 12]
17 : [1 2 3] [6 8 9 11 13 13 15 17 19 20 21] [27 28 29 30 32 33 35 35] [38 38 40] [43 45 45 46] [49]
18 : [3 3 3] [8 10 11 11 11 12 14] [18] [21 22 23] [29] [32 33 34 34 34 35 37 38 39 41] [44 46 48 49 49]
18 : [1 2 3] [6] [9] [12 13 15 17 18 19 20 21 21 23 24 25 25] [35 36] [40 41 41 41 43 45 46 46 46] [49]
16 : [1 3] [6 6 6 6] [11 12 14 14 15 17 19 20 20 21 21 22] [25 25 27 29 31 32 33] [38 39] [44 45] [49]
16 : [2 4] [7 7 8 10 12 13 15 16] [22 22 24 24] [27 29 31 31 33 34] [37 38 39] [42 43 43 44 45 46 47]
17 : [2 4 5 6 7] [11 11 13 13 14 15 16 17 17 17 19] [29] [34 35 36] [39 39 41 41 41 42 44 46] [49 49]
18 : [3 4 5 7 7] [10 10 12 12 12 14 15 15 17 18] [21] [24 24] [28] [32] [39 40 41 42 43 44 44] [47 47] [50]
16 : [2 4] [7 7 8 8] [11 11] [14 16 17 17 18 19] [22 24 26 26] [30 31 33 34 34 35] [38 38 39] [42 43] [50]
16 : [1 3 4 5] [11 11] [15 15 17 18 19 21 22 23 23 25 27 27 27 27 28 29 30 30] [33 34] [41 41] [45] [48]
17 : [2 2 3 4 6 6 7] [10 10] [13 14 15 16 17 19] [23 25] [28 30 31 32 33 34 36 37 38] [42] [48 49 50]
17 : [2] [7 9 9 9 9 10 10 12] [16 16] [19 21 21 22 24] [27 27 27] [36 36 36 37 39 39 40 40 40 41] [46]
18 : [1] [5 6 6 8] [11 11 12] [19 19 20 21 22 24 26 26] [29 30 31 32 34 35 35] [38] [42] [45] [48 48 49 49]
16 : [2 4 4 6] [11 12 13 13 13] [21 21 21 23] [30 31 31 33 35] [41 41 41 43 45 46 47 48 48 49 49 50]
16 : [2 2] [8 10 12] [15 15 15 15 16 16] [19 20] [23 24] [28 28 29] [32 34 36 36 36 37 39 41] [44 45 47 48]
17 : [3 3] [6] [9 10 11] [17 18] [21 23 23] [27 28 29 29 30 31 31 33] [37 37 39 39 39 40] [43 44] [47 48 49]
17 : [4] [7 9 10 10] [14 14 14] [17] [21] [25 25 27 27 28 30] [33 35 37 37 38 40 41 43 44 45 47 48 49 50]
18 : [3 4 5 6 7] [10 11 12 12 14 15 16 17] [20] [23 24 25 25 26 26] [31] [35] [38 40 41 42] [45 46 47] [50]
17 : [1 3] [8 10] [16 16 18 19 20 20] [23 23] [26] [30 31 33 34 35] [39 39 39 40 41 42 43] [46 46 47 47 49]
18 : [2 4 4 4 4 6 7 8 8 10 10] [13] [16 17] [20 22 23 25 25] [29 29 29] [33] [39 40 42] [48 48 49 49]
16 : [1 1 3 4] [7 8 10 10] [18 18 20 21] [24 25 26 27 29 31 33 33 34 34] [37 37 39] [45 46 48 49 49]
17 : [1] [4 4] [7 9 9 11 12] [15 16 17 17 18 19 21 21 21 22 23] [27 28 30 31] [37 39] [42] [48 49 49 50]
17 : [3 4 6 7 7 8 9 10 10 11 13 14 14] [21 21 23] [26 27] [31 32] [35 36] [39 40 41 41 41] [44 44] [49]
16 : [1] [4 6 6 8 10 12 13 15] [20 20 21 21] [29 29 30] [34 36 36 37 37 38 38 40] [44 45 46 47 47 48]
17 : [3 4 4 6] [12 14 15 16 17] [20 21 22 22 22 23 24 26 26] [29 30 32] [35 37 37 37 38 39 41 42] [48]
19 : [1] [5] [8 9] [14 14 14 16 16 17 17 17 17] [21] [24 24 24] [30] [34 35 36 37 39 40 40] [45 46 46 47 48]

1

Mathematica,67个字节

Length@Flatten[Partition[#,UpTo@2]&/@Split[Sort@#,Abs[#-#2]<3&],1]&

Wolfram沙箱中尝试。


有什么办法可以测试吗?喜欢Wolfram的东西吗?
LiefdeWen

@LiefdeWen您可以在线尝试!在数学中。数学不支持Wolfram语言的所有功能,但是本条目中使用的所有功能都已实现,因此数学被破坏或该解决方案无效。
Pavel

它可以在sandbox.open.wolframcloud.com上运行,所以问题出在数学方面
ovs

1
@凤凰不认为数学会支持UpTo
马丁

0

Perl,103个字节

say sub{for(1..$#_+1){$x{$i}++;$i++if$_[$_]-$_[$_-1]>2}@_/2+.5*grep$_%2,values%x}->(sort{$a<=>$b}@ARGV)

从命令行获取参数列表(如@ARGV),STDOUT默认情况下输出到。

这是一种替代方法,基于以下关系:

Minimum pairs = ( Population size + # Odd neighbourhoods ) / 2

(有关如何定义邻居的信息,请参见此答案


0

Javascript,67个字节

a=>(a=a.sort((a,b)=>a-b)).filter((n,i)=>m=!m|n-a[i-1]>2,m=0).length

示例代码段:

f=
a=>(a=a.sort((a,b)=>a-b)).filter((n,i)=>m=!m|n-a[i-1]>2,m=0).length

v=[[2,4,6,6,8,14],[2,1,3,1,1],[4,1,4,9,1,8,9,1,8,4],[1,2,3,5,7,8,10,12]]
for(k=0;k<4;k++)
  console.log(`f([${v[k]}])=${f(v[k])}`)

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.