牛客练习赛95 C-Division

本文最后更新于:3 years ago

题目链接:点击跳转

题意:给出一个长度为n的数组,每次可以取长度大于等于k的一段对里面的数除二(向下取整),问能不能将里面所有数变为1

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def get_ans(self):  # 计算该数几次操作后变为1
ans = -1 # 因为是变为1,所以要减去一次
while self:
ans += 1
self >>= 1
return ans


def main():
T = int(input())
N = 10005
num = [0] * N
for t in range(T):
n, k = map(int, input().split())
s = input().split()
for i in range(n):
num[i] = get_ans(int(s[i]))
f = True
res = []
cnt = 0
i = 0
while i < n:
if num[i] == 0:
i += 1
else:
j = i
while j < n and (j == 0 or num[j] > num[j - 1]):
num[j] -= 1
j += 1
if j - i < k: # 注意在while结束的j是多了1的,就不用在加上1了
f = False
print(-1)
break
cnt += 1
res.append(i + 1)
res.append(j)
if f:
print(cnt)
for i in range(0, cnt << 1, 2):
print('%d %d' % (res[i], res[i + 1]))


if __name__ == '__main__':
main()