输入包括两行,第一行包括一个整数n(1 ≤ n ≤ 10^5),即数列的长度; 第二行n个整数a_i, 表示数列中的每个数(1 ≤ a_i ≤ 10^9),以空格分割。
输出一个整数,表示最长的长度。
6 7 2 3 1 5 6
5
import sys def fun(values): i = 1 record=[] while (i <= len(values) - 2): if(values[i - 1] > values[i] and values[i] < values[i + 1]&nbs***bsp; ### through of wave (values[i-1] < values[i] and values[i] > values[i + 1])): ### peak of wave if (values[i + 1] >= values[i - 1]): ######### right > left j = i - 1 while(j>=1 and values[j-1]<=values[j]): # find left longest subsequence j=j-1 k=i+2 while (k <= len(values) - 1 and values[k] >= values[k - 1]): # find right longest subsequence k+= 1 if(len(record)==0):record.append((j,k-j)) ####for the subsequence: subscript is j, superscript is k-1 else: s_index,s_len=record[0] if(k-j>s_len): record[0]=(j,k-j) i+=1 ################# check each element, rather than check continuous 3 elements final_index,final_len=record[0] # print(final_index,values[final_index],final_len) print( final_len) if __name__ == "__main__": n = int(sys.stdin.readline().strip()) line = sys.stdin.readline().strip() values = list(map(int, line.split())) fun(values)
def findString():
length = input("请输入长度:")
string = input("请输入%s位数列(空格分开):"%length)
strList = string.split(" ")
resultList = []
for i in range(0,eval(length)):
n = 0
result = strList[i]
for s in strList[i+1:]:
if s>result[len(result)-1]:
result+=s
elif s <result[len(result)-1] and n<1:
result+=str(eval(result[len(result)-1])+1)
n+=1
else:
break
resultList.append(result)
resultLen=len(resultList[0])
for i in range(0,len(resultList)):
print(resultList[i])
if len(resultList[i])>resultLen:
resultLen = len(resultList[i])
print(resultLen)
findString()
import sys def Main(strs): n = int(strs) li = map(int,sys.stdin.readline().strip().split()) if n == 1: return 1 num = [] count = 1 maxval = 0 for i in xrange(1,n): if li[i]<=li[i-1]: num.append(count) count = 0 count += 1 if count !=0: num.append(count) if len(num) == 1: return num[0] sumval = 0 for i in xrange(len(num)-1): sumval += num[i] if num[i+1] != 1: if (li[sumval-1]+1<li[sumval+1] or li[sumval-2]+1<li[sumval]) and num[i] + num[i+1] > maxval: maxval = num[i]+ num[i+1] else: if num[i] + 1 > maxval: maxval = num[i]+ num[i+1] return maxval while True: strs = sys.stdin.readline().strip() if not strs: break print Main(strs)