所以我试图用我的代码解决问题,根据我的计算,该代码应在指定的期限内工作 链接: https://www.spoj.com/problems/CWC2015/
我已经尝试过尽可能多地优化它,我可以将python中的集合用作哈希图
from operator import itemgetter
def sumarr(arr):
sum=0
for num in arr:
sum+=num
return(sum,len(arr))
def subsum(arr):
sum=[[]]
for num in arr:
sum=sum+[x+[num] for x in sum]
return sum
t=int(input())
for x in range(t):
flag=0
n=int(input())
exp=list(map(int,input().split()))
if(n%2==1):
print("Case "+str(x+1)+": "+"No")
else:
total=sumarr(exp)[0]
if(total%2==0):
one=exp[:n//2]
two=exp[n//2:]
list1=subsum(one)
list1p=[]
for lis in list1:
list1p.append(sumarr(lis))
list2=subsum(two)
list2p=[]
for lis in list2:
list2p.append(sumarr(lis))
set2=set(list2p)
for num in list1p:
if(num[0]==total//2):
if(num[1]==n//2):
flag=1
break
elif(num[0]<total//2):
if (total//2-num[0],n//2-num[1]) in set2:
flag=1
break
if(flag==1):
print("Case "+str(x+1)+": "+"Yes")
else:
print("Case "+str(x+1)+": "+"No")
else:
print("Case "+str(x+1)+": "+"No")
我希望它会被接受。