fork download
  1. E=enumerate
  2. L=len
  3. def f(l,I=[],r=[]):
  4. if L(I)==L(l):yield r;return
  5. for i,a in E(l):
  6. if(i in I)<a==1:
  7. j,k,Q,W=i,i,[],[]
  8. while(j:=j-1)>=0==(j in I)and~-l[j]*(Q==[]or Q[-1][0]<l[j]):Q+=(l[j],j),
  9. while(k:=k+1)<L(l)and~-(k in I)*~-l[k]*(W==[]or W[-1][0]<l[k]):W+=(l[k],k),
  10. for x in range((A:=max(L(Q),L(W)))*A):
  11. for o,V in E(u:=[Q[:x//A+1],W[:x%A+1]]):
  12. if V and V[0][0]==2:
  13. M=V+u[1>o]
  14. if L(S:=[X for X,_ in M])==L({*S}):yield from f(l,I+[j for _,j in M]+[i],r+[M+[(a,i)]])
  15. yield from f(l,I+[i],r+[[(1,i)]])
  16. def F(l):
  17. R=[];B={str(sorted(j))for j in f(l)}
  18. for i in map(eval,B):
  19. v=[-1]*L(l)
  20. for P,j in E(i):
  21. for _,I in j:v[I]=P
  22. R+=v,
  23. return{*map(tuple,R)}
  24.  
  25. print(F([1,2,1]))
  26. print(F([2,1,3,2,1]))
  27. print(F([1,2,1,2,1]))
  28. print(F([1,6]))
  29. print(F([4,2,3,1]))
  30. print(F([1]))
  31. print(F([1,2,3,4]))
  32. print(F([4,3,1,2]))
  33. print(F([3,1,2,4]))
  34. print(F([1,2,1,3]))
  35. print(F([1,2,2,1]))
  36. print(F([1, 2, 4, 3]))
Success #stdin #stdout 0.08s 14140KB
stdin
Standard input is empty
stdout
{(1, 1, 0), (0, 1, 1)}
{(0, 0, 0, 1, 1), (0, 0, 1, 1, 1)}
{(0, 1, 1, 2, 2), (1, 1, 0, 2, 2), (1, 1, 2, 2, 0)}
set()
set()
{(0,)}
{(0, 0, 0, 0)}
{(0, 0, 0, 0)}
{(0, 0, 0, 0)}
{(0, 1, 1, 1)}
{(0, 0, 1, 1)}
set()