第一行输入两个整数
代表数组中的元素数量。
第二行输入
个整数
代表数组元素。
在一行上输出一个整数,代表切分方案数。
3 3 3 3
1
6 1 1 4 5 1 4
0
10 0 3 4 2 3 2 1 -1 3 4
2
while 1: try: n = int(input()) ls = list(map(int,input().split())) if sum(ls)%3 != 0: print(0) break ans1 = sum(ls)//3 ans2 = ans1*2 sumls = [0] pls = [0] for i in range(n): sumls.append(sumls[-1]+ls[i]) if ls[i]>0: pls.append(pls[-1]+1) else: pls.append(pls[-1]) sumls = sumls[1:] pls = pls[1:] count = 0 ans1ind = [] ans2ind = [] for i in range(n): if sumls[i]==ans1 and pls[i]>0: ans1ind.append(i) elif sumls[i]==ans2 and pls[-1]-pls[i]>0: ans2ind.append(i) for x in ans1ind: for y in ans2ind: if x<y and pls[y]-pls[x]>0: count += 1 print(count) break except: break
num = int(input())
num_list = input().split(" ")
num_list = [int(item) for item in num_list]
obj = sum(num_list) / 3
if not obj == int(obj):
print(0)
exit(0)
sum_prefix = [0] * num
count_prefix = [0] * num
loc_dic = {"m": [], "2m": []}
temp = 0
count = 0
for i in range(0, num):
if num_list[i] > 0:
count += 1
temp = temp + num_list[i]
sum_prefix[i] = temp
count_prefix[i] = count
if temp == obj:
loc_dic["m"].append(i)
if temp == 2 * obj:
loc_dic["2m"].append(i)
res = 0
for L in loc_dic["m"]:
for R in loc_dic["2m"]:
if L < R and count_prefix[R] - count_prefix[L] > 0:
res += 1
print(res) 最后一个过不了, 超时了