Long and complex....!!!
==================================================================
import sys
def recurse(s,dim,func,mode="all",exec_all_dim=False,level=0,val=[],skip=False):
if level<dim:
if(exec_all_dim) and val!=[]:
func(val)
upp = len(s)
low = 0
forw = s
if mode=="single":
upp = 1
forw = s[1:]
for i in range(low,upp):
if mode=="upper":
forw = s[i+1:]
elif mode=="exclude":
forw = s[:i]+s[i+1:]
elif mode=="upper_include":
forw = s[i:]
if skip:
recurse(forw,dim,func,mode,exec_all_dim,level+1,val,skip=skip)
recurse(forw,dim,func,mode,exec_all_dim,level+1,val+[s[i]],skip=skip)
elif level==dim and val!=[]:
func(val)
n = int(input())
pos = [[int(input()),int(input())] for i in range(n)]
bat = int(input())
all_comb = []
max_saves = []
saves = []
def exec(val):
posit = [val[i][0] for i in range(len(val))]
if sum(posit)<=bat:
all_comb.append(posit)
saves.append(sum([val[i][1] for i in range(len(val))]))
elif posit == [pos[i][0] for i in range(len(pos))] and all_comb==[] and sum(posit)<bat:
all_comb.append([pos[i][0] for i in range(len(pos))])
saves.append(sum([pos[i][1] for i in range(len(pos))]))
if posit == [pos[i][0] for i in range(len(pos))]:
for i in range(len(all_comb)):
if saves[i]==max(saves):
max_saves.append(all_comb[i])
min_moves = min([abs(min(max_saves[i])-max(max_saves[i])) for i in range(len(max_saves))]) if max_saves!=[] else 0
for i in range(len(max_saves)):
if abs(min(max_saves[i])-max(max_saves[i])) == min_moves:
max_saves[i].sort()
print(max_saves[i])
sys.exit()
recurse(pos,len(pos),exec,mode="single",skip=True)
==================================================================
import sys
def recurse(s,dim,func,mode="all",exec_all_dim=False,level=0,val=[],skip=False):
if level<dim:
if(exec_all_dim) and val!=[]:
func(val)
upp = len(s)
low = 0
forw = s
if mode=="single":
upp = 1
forw = s[1:]
for i in range(low,upp):
if mode=="upper":
forw = s[i+1:]
elif mode=="exclude":
forw = s[:i]+s[i+1:]
elif mode=="upper_include":
forw = s[i:]
if skip:
recurse(forw,dim,func,mode,exec_all_dim,level+1,val,skip=skip)
recurse(forw,dim,func,mode,exec_all_dim,level+1,val+[s[i]],skip=skip)
elif level==dim and val!=[]:
func(val)
n = int(input())
pos = [[int(input()),int(input())] for i in range(n)]
bat = int(input())
all_comb = []
max_saves = []
saves = []
def exec(val):
posit = [val[i][0] for i in range(len(val))]
if sum(posit)<=bat:
all_comb.append(posit)
saves.append(sum([val[i][1] for i in range(len(val))]))
elif posit == [pos[i][0] for i in range(len(pos))] and all_comb==[] and sum(posit)<bat:
all_comb.append([pos[i][0] for i in range(len(pos))])
saves.append(sum([pos[i][1] for i in range(len(pos))]))
if posit == [pos[i][0] for i in range(len(pos))]:
for i in range(len(all_comb)):
if saves[i]==max(saves):
max_saves.append(all_comb[i])
min_moves = min([abs(min(max_saves[i])-max(max_saves[i])) for i in range(len(max_saves))]) if max_saves!=[] else 0
for i in range(len(max_saves)):
if abs(min(max_saves[i])-max(max_saves[i])) == min_moves:
max_saves[i].sort()
print(max_saves[i])
sys.exit()
recurse(pos,len(pos),exec,mode="single",skip=True)
No comments:
Post a Comment