6 条题解
-
0
用python做的,前面一部分判断在最左边和最右边的情况,然后分析不在边界的情况,用二分法找到其中一个值的时候,再用循环分别向前找和向后找并记录位置
n,q = map(int,input().split()) num_list = list(map(int,input().split())) quest_list=[] def check(num): global num_list begin = 0 end = len(num_list) - 1 pos_begin = 0 pos_end = 0 flag = True if num_list[begin] == num: flag = False count = 0 pos_begin = 0 while (num_list[count] == num): pos_end = count count += 1 elif num_list[end] == num: flag = False count = len(num_list) - 1 pos_end = len(num_list) - 1 while (num_list[count] == num): pos_begin = count count -= 1 else: while((end - begin) != 1): middle = (begin + end) // 2 if num > num_list[middle]: begin = middle elif num <num_list[middle]: end = middle else: flag = False count1 = middle count2 = middle while(num_list[count1] == num): pos_begin = count1 count1 -= 1 while(num_list[count2] == num): pos_end = count2 count2 += 1 break if flag: pos_begin = -1 pos_end = -1 print(pos_begin,pos_end) for i in range(q): quest_list.append(int(input())) for quest_num in quest_list: check(quest_num)
信息
- ID
- 88
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- 递交数
- 656
- 已通过
- 150
- 上传者