7.先升后降

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhou_pp/article/details/85161889

Description

从一列数中筛除尽可能少的数使得从左往右看,这些数是从小到大再从大到小的。

Input

输入时一个数组,数值通过空格隔开。

Output

输出筛选之后的数组,用空格隔开。如果有多种解雇哦,则一行一种结果。

Sample Input 1 

1 2 4 7 11 10 9 15 3 5 8 6

Sample Output 1

1 2 4 7 11 10 9 8 6

def fun(arr):
    length = len(arr)
    lists = [[] for i in range(length)]
    for j in range(length):
        a = arr[j:]
        alength = len(a)
        if alength < 2:
            if alength == 0:
                continue
            elif alength == 1:
                lists[j].append(a[0])
        else:
            for i in range(alength):
                if i == 0:
                    lists[j].append(a[0])
                elif i == 1:
                    if a[1] < a[0]:
                        continue
                    else:
                        lists[j].append(a[1])
                else:
                    if len(lists[j]) == 1:
                        if a[i]>lists[j][0]:
                            lists[j].append(a[i])
                        else:
                            continue
                    else:
                        if a[i] < lists[j][len(lists[j]) - 1] and a[i] > lists[j][len(lists[j]) - 2]:
                            lists[j].pop()
                            lists[j].append(a[i])
                        elif a[i] < lists[j][len(lists[j]) - 1] and a[i] < lists[j][len(lists[j]) - 2]:
                            continue
                        else:
                            lists[j].append(a[i])
    return lists

def maxLength(a):
    maxlength = 0
    for list in a:
        maxlength = max(maxlength, len(list))
    return maxlength

def resultLists(a):
    numList = []
    maxlength = maxLength(a)
    for list in a :
        if len(list) == maxlength:
            numList.append(list)
    return numList

def prinrtFormate(arr):
    resultstr = ""
    for num in arr:
        resultstr = resultstr + " " + str(num)
    resultstr = resultstr[1:]
    return resultstr

if __name__ == "__main__":
   arr = input()
   a = [int(n) for n in arr.split(" ")]
   allarr = []
   maxarrlength = 0
   for i in range(len(a)):
       b = a[0:i+1]
       c = a[i + 1:]
       c = c[::-1]
       numList1 = resultLists(fun(b))
       numList2 = resultLists(fun(c))
       numList3 = []
       for list in numList2:
           list = list[::-1]
       numList3.append(list)
       for list1 in numList1:
           for list3 in numList3:
              allarr.append(list1+list3)
              maxarrlength = max(len(list1+list3),maxarrlength)
   allarr2 = []
   for element in allarr:
     if (element not in allarr2):
         allarr2.append(element)
   for list in allarr2:
      if len(list) == maxarrlength:
         print(prinrtFormate(list))

猜你喜欢

转载自blog.csdn.net/zhou_pp/article/details/85161889