Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. test1 = True
  2. test2 = True
  3.  
  4. def main():
  5. inputs = ["a_example.in", "b_small.in", "c_medium.in", "d_quite_big.in", "e_also_big.in"]
  6. for x in inputs:
  7. f = open(x)
  8. slices_all, pizzas = f.readline().split()
  9. slices = list(map(int, (f.readline().split())))
  10. out = solve(slices_all,slices)
  11. out.reverse()
  12. print(len(out) , "\n", *out)
  13. out_name, b= x.split(".")
  14. out_f = open(out_name + ".out", 'w')
  15. out_f.write(str(len(out)) + '\n')
  16. for x in out:
  17. out_f.write(str(x) + " ")
  18. out_f.close()
  19.  
  20.  
  21. def solve(sum, slices):
  22. global test1
  23. global test2
  24. out = []
  25. score = 0
  26. i = len(slices) -1
  27. while i>=0 and test1:
  28. temp_out = []
  29. curr_sum = 0
  30. j=i
  31. while j>=0 and test2:
  32. tmp = slices[j]
  33. if curr_sum + tmp < int(sum):
  34. curr_sum += tmp
  35. temp_out.append(j)
  36. elif curr_sum + tmp == int(sum):
  37. curr_sum += tmp
  38. temp_out.append(j)
  39. test2 = False
  40. test1 = False
  41. j -= 1
  42. if curr_sum > score:
  43. score = curr_sum
  44. out = temp_out
  45. if len(out) == len(slices):
  46. test1 = False
  47. i -= 1
  48.  
  49. print(score)
  50. return out
  51.  
  52.  
  53. if __name__ == "__main__":
  54. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement