Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. from numba import njit
  2. import numpy as np
  3.  
  4.  
  5. class Problem:
  6. def __init__(self, problem):
  7. self.problem = problem
  8. self.output = None
  9. self.B, self.L, self.D, self.S, self.N, self.T, self.M, self.idx = self.read_data()
  10. self.remaining_libraries = np.array(list(range(self.L)))
  11. self.current_day = 0
  12.  
  13. def read_data(self):
  14. filename = f'input/{self.problem}.txt'
  15. lines = [line.strip('\n') for line in open(filename, 'r')]
  16. B, L, D = map(int, lines[0].split())
  17. S = np.array(list(map(int, lines[1].split())))
  18.  
  19. N = np.zeros(L)
  20. T = np.zeros(L)
  21. M = np.zeros(L)
  22. idx = -np.ones(shape=(L, B), dtype=int)
  23. for i in range(L):
  24. n, t, m = map(int, lines[2*i + 2].split())
  25. N[i] = n
  26. T[i] = t
  27. M[i] = m
  28. books = np.array(list(map(int, lines[2*i + 3].split())))
  29. book_values = S[books]
  30. sorted_idx = np.argsort(-book_values)
  31. idx[i][:books.shape[0]] = books[sorted_idx]
  32.  
  33. return B, L, D, S, N, T, M, idx
  34.  
  35. def run(self):
  36. info, books = njit_run(self.current_day, self.remaining_libraries, self.D, self.N, self.T, self.M, self.S, self.idx)
  37. return info, books
  38.  
  39. def save(self, info, books):
  40. with open(f'output/{self.problem}.out', 'w') as f:
  41. for i in range(len(info)):
  42. for arr in [info, books]:
  43. out = ' '.join(map(str, arr[i]))
  44. f.write(f'{out}\n')
  45.  
  46.  
  47. @njit(fastmath=True)
  48. def njit_run(current_day, remaining_libraries, D, N, T, M, S, idx):
  49. info = []
  50. books = []
  51. score = 0
  52. while current_day < D:
  53. best_library, best_library_value, best_books = calculate_best_library(remaining_libraries, current_day, D, N, T, M, S, idx)
  54. if best_library_value <= 0:
  55. break
  56. score += best_library_value
  57. current_day += T[best_library]
  58. remaining_libraries[best_library] = -1
  59. remove_books(remaining_libraries, set(best_books), idx)
  60. if best_books.shape[0] != 0:
  61. info.append([best_library, best_books.shape[0]])
  62. books.append(best_books)
  63. print('Progress:', current_day/D)
  64. print('Score:', score)
  65. return info, books
  66.  
  67.  
  68. @njit(fastmath=True)
  69. def calculate_best_library(remaining_libraries, current_day, deadline, N, T, M, S, idx):
  70. best_library_value = -1
  71.  
  72. for lib in remaining_libraries:
  73. if lib == -1:
  74. continue
  75. constructed_by = current_day + T[lib]
  76. remaining_days = max(0, deadline - constructed_by)
  77. lib_size = np.where(idx[lib] == -1)[0][0]
  78. books_by_deadline = int(min(lib_size, M[lib]*remaining_days))
  79. books = idx[lib][:books_by_deadline]
  80.  
  81. library_value = 0
  82. for b in books:
  83. library_value += S[b]
  84.  
  85. if library_value > best_library_value:
  86. best_library = lib
  87. best_library_value = library_value
  88. best_books = books
  89.  
  90. return best_library, best_library_value, best_books
  91.  
  92.  
  93. @njit(fastmath=True)
  94. def remove_books(remaining_libraries, best_books, idx):
  95. for lib in remaining_libraries:
  96. if lib == -1:
  97. continue
  98. lib_size = np.where(idx[lib] == -1)[0][0]
  99. for i in range(lib_size):
  100. if idx[lib][i] in best_books:
  101. idx[lib][i] = -1
  102. new_idx = np.where(idx[lib] != -1)[0]
  103. idx[lib][:new_idx.shape[0]] = idx[lib][new_idx]
  104. idx[lib][new_idx.shape[0]:] = -1
  105.  
  106.  
  107. if __name__ == '__main__':
  108. for problem in ['c']:
  109. print(problem)
  110. cls = Problem(problem)
  111. info, books = cls.run()
  112. cls.save(info, books)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement