一个上升,另一个下降


20

介绍

在这个挑战中,您的任务是确定给定的数字序列是否可以分为两个子序列,其中一个在增加,另一个在减少。例如,考虑序列8 3 5 5 4 12 3。它可以分为两个子序列,如下所示:

  3 5 5   12
8       4    3

第一行的子序列增加,第二行的子序列减少。此外,您应该有效地执行此任务。

输入项

您的输入是一个非空L的整数列表,范围在0到99999(含)之间。它以您的语言的本机格式给出,或仅由空格分隔。

输出量

如果L可以分解为递增和递减子序列,则输出为真值,否则为假值。子序列不必严格增加或减少,它们中的任何一个都可以是空的。

规则和奖金

您可以编写完整的程序或函数。最低字节数获胜,并且不允许出现标准漏洞。此外,在此挑战中禁止强行使用:您的程序必须在输入长度内以多项式时间运行

您不需要实际返回这两个子序列,但是这样做有-20%奖金。为了使奖金更容易以静态类型的语言申领,可以为伪造的实例返回一对空列表。

测试用例

input -> None以错误输入和input -> inc dec真实输入的格式给出。这里只给出一对可能的子序列。可能还有更多。

[4,9,2,8,3,7,4,6,5] -> None
[0,99999,23423,5252,27658,8671,43245,53900,22339] -> None
[10,20,30,20,32,40,31,40,50] -> None
[49,844,177,974,654,203,65,493,844,767,304,353,415,425,857,207,871,823,768,110,400,710,35,37,88,587,254,680,454,240,316,47,964,953,345,644,582,704,373,36,114,224,45,354,172,671,977,85,127,341,268,506,455,6,677,438,690,309,270,567,11,16,725,38,700,611,194,246,34,677,50,660,135,233,462,777,48,709,799,929,600,297,98,39,750,606,859,46,839,51,601,499,176,610,388,358,790,948,583,39] -> None
[0,1,2,3,4] -> [0,1,2,3,4] []
[4,3,2,1,0] -> [] [4,3,2,1,0]
[1,9,2,8,3,7,4,6,5] -> [1,2,3,4,6] [9,8,7,5]
[71414,19876,23423,54252,27658,48671,43245,53900,22339] -> [19876,23423,27658,48671,53900] [71414,54252,43245,22339]
[10,20,30,20,30,40,30,40,50] -> [10,20,20,30,40,40,50] [30,30]
[0,3,7,13,65,87,112,43,22,1] -> [0,3,7,13,65,87,112] [43,22,1]
[7,4,4,7,4,7,7,4,7,4,4,4,7,7] -> [7,7,7,7,7,7,7] [4,4,4,4,4,4,4]
[7,997,991,957,956,952,7,8,21,924,21,923,22,38,42,44,920,49,58,67,71,83,84,85,917,89,907,896,878,878,90,861,115,860,125,128,140,148,858,155,160,836,164,182,826,191,824,805,195,792,205,782,206,210,769,213,756,748,214,745,724,701,234,241,693,268,685,293,679,297,334,671,336,669,341,652,356,648,362,364,370,375,386,630,622,388,389,618,398,408,468,615,470,533,611,539,544,609,586,582,572,565,547,602,536,619,624,528,512,631,640,649,669,671,677,505,678,723,743,489,489,473,454,757,446,445,758,759,764,445,431,770,429,426,418,409,790,383,379,366,363,791,358,795,809,827,835,356,353,841,844,333,867,323,317,879,311,881,309,896,282,281,897,263,904,237,236,226,202,195,914,186,177,917,920,157,926,936,154,138,943,131,945,100,98,947,957,964,95,973,989,57,43,32,21,16,13,11,8,0] -> [7,7,8,21,21,22,38,42,44,49,58,67,71,83,84,85,89,90,115,125,128,140,148,155,160,164,182,191,195,205,206,210,213,214,234,241,268,293,297,334,336,341,356,362,364,370,375,386,388,389,398,408,468,470,533,539,544,586,602,619,624,631,640,649,669,671,677,678,723,743,757,758,759,764,770,790,791,795,809,827,835,841,844,867,879,881,896,897,904,914,917,920,926,936,943,945,947,957,964,973,989] [997,991,957,956,952,924,923,920,917,907,896,878,878,861,860,858,836,826,824,805,792,782,769,756,748,745,724,701,693,685,679,671,669,652,648,630,622,618,615,611,609,582,572,565,547,536,528,512,505,489,489,473,454,446,445,445,431,429,426,418,409,383,379,366,363,358,356,353,333,323,317,311,309,282,281,263,237,236,226,202,195,186,177,157,154,138,131,100,98,95,57,43,32,21,16,13,11,8,0] 

Answers:


3

Pyth,34个字节

.N|!N|&ghNT:tNhNY&gYhN:tNThN:QZ^T5

测试套件

使用建议的递归来减少运行时间。定义一个3输入函数:,它接受输入`list后缀,递增序列的结尾,递减序列的结尾。


2

Brachylog,16个字节-20%= 12.8(但几乎可以肯定不是多项式)

⊇≥₁X&⊇≤₁Y;X.cp?∧

在线尝试!

如果没有一对兼容的子序列,则失败,如果有一对子序列,则通过其输出变量输出它们(但true.如果它作为程序运行,则只会打印出来)。我说这几乎不是多项式,因为Brachylog的优点在于,由于它是一种声明性语言,因此您无需像描述变量之间的关系并要求计算机计算结果那样在实现算法方面做很多工作。因此,这很有可能是顽固的蛮力,但是我花了足够长的拷贝粘贴测试用例(其中两个超时),我觉得我应该以任何方式提交此测试,如果没有其他原因,那就是将这个挑战拖累了一下。从 “最新”列表的后面。

   X                X is a
 ≥₁                 non-increasing
⊇                   sublist of the input
    &               and
        Y           Y is a
      ≤₁            non-decreasing
     ⊇              sublist of the input
         ;X         which paired with X
           .        is the output variable
            c       which when its elements are concatenated
             p      is a permutation of
              ?     the input
               ∧    which is not unified with the output.

2

Haskell,65个字节

(>[]).foldl(%)[(0,9^6)]
p%x=do(u,d)<-p;[(x,d)|x>=u]++[(u,x)|x<=d]

在线尝试!

遍历该列表,跟踪(u,d)递增序列的最大值和递减序列的最小值的可能对。每个新元素都会x替换ud,这相当于将其追加到该子序列。可能两个选项都无效。最后,我们检查可能性列表是否为空。

最初的范围(0,9^6)使用问题指定号码是范围为0 - 99999。更通用的解决方案可以做(1/0,-1/0),以品牌(-inf,inf)

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.