|
1 | | -class BookScanner: |
2 | | - def __init__(self, file): |
3 | | - self.file = file |
4 | | - f = open(self.file, "r") |
5 | | - self.books, self.libraries, self.days = tuple(int(i) for i in f.readline().rstrip().split(" ")) |
6 | | - |
7 | | - # library_data = tuple(([], set()) for i in range(libraries)) |
8 | | - |
9 | | - self.library_data = {i: [] for i in range(self.libraries)} |
10 | | - self.library_books = {i: set() for i in range(self.libraries)} |
11 | | - self.book_scores = [int(i) for i in f.readline().rstrip().split(" ")] |
12 | | - self.reamining_days = self.days + 10 |
13 | | - |
14 | | - # 扫图书馆 |
15 | | - for i in range(self.libraries): |
16 | | - for j in f.readline().rstrip().split(" "): |
17 | | - self.library_data[i].append(int(j)) |
18 | | - for j in f.readline().rstrip().split(" "): |
19 | | - self.library_books[i].add(int(j)) |
20 | | - f.close() |
21 | | - |
22 | | - self.already_sign_up = set() |
23 | | - self.library_score_queue = [i for i in range(self.libraries)] |
24 | | - self.result = [[self.libraries]] # + [[] for i in range(self.libraries * 2)] |
25 | | - |
26 | | - def operate(self): |
27 | | - i = 0 |
28 | | - while i < self.result[0][0]: |
29 | | - |
30 | | - # self.library_score_queue = sorted(self.library_score_queue, |
31 | | - # key=lambda x: -sum([self.book_scores[i] for i in self.library_books[x]]) * |
32 | | - # (self.library_data[x][2] / (self.library_data[x][1] ** 2))) |
33 | | - |
34 | | - self.library_score_queue = sorted(self.library_score_queue, |
35 | | - key=lambda x: -sum([self.book_scores[k] for k in self.library_books[x]]) / |
36 | | - ((self.reamining_days - self.library_data[x][1]) / |
37 | | - self.library_data[x][2])) |
38 | | - |
39 | | - tmp = self.library_books[self.library_score_queue[0]] - self.already_sign_up |
40 | | - if len(tmp) == 0: |
41 | | - self.result[0][0] = self.result[0][0] - 1 |
42 | | - # self.result.pop() |
43 | | - # self.result.pop() |
44 | | - continue |
45 | | - self.result.append([]) |
46 | | - self.result.append([]) |
47 | | - self.result[(i + 1) * 2 - 1].append(self.library_score_queue[0]) # 图书馆编号 |
48 | | - # result[(i + 1) * 2 - 1].append(len(library_books[library_score_queue[i]])) # 书数量 |
49 | | - # result[(i + 1) * 2] = list(library_data[library_score_queue[i]]) |
50 | | - # |
51 | | - self.result[(i + 1) * 2 - 1].append(len(tmp)) |
52 | | - # self.result[(i + 1) * 2] = list(tmp) |
53 | | - self.result[(i + 1) * 2] = sorted(tmp, key=lambda x: -self.book_scores[x]) |
54 | | - |
55 | | - # 去重 |
56 | | - self.already_sign_up = self.already_sign_up | tmp |
57 | | - for j in self.already_sign_up: |
58 | | - self.book_scores[j] = 0 |
59 | | - # print("already_sign_up: " + str(already_sign_up)) |
60 | | - |
61 | | - print(self.file, i) |
62 | | - |
63 | | - # 判断超时 |
64 | | - self.reamining_days -= self.library_data[self.library_score_queue[0]][1] |
65 | | - if self.reamining_days < 0: |
66 | | - self.result[0][0] = int((len(self.result) - 1) / 2) |
67 | | - break |
68 | | - |
69 | | - i += 1 |
70 | | - self.library_score_queue.pop(0) |
71 | | - |
72 | | - def write_file(self): |
73 | | - print(self.library_data) |
74 | | - print(self.result) |
75 | | - output = self.result |
76 | | - |
77 | | - f = open(self.file.replace("txt", "out1"), "w") |
78 | | - for i in output: |
79 | | - f.write(" ".join([str(j) for j in i])) |
80 | | - f.write("\n") |
81 | | - |
82 | | - f.close() |
83 | | - |
84 | | - # @staticmethod |
85 | | - def __call__(self): |
86 | | - # f = BookScanner(BookScanner) |
87 | | - self.operate() |
88 | | - self.write_file() |
89 | | - |
90 | | - |
91 | | -def run_it(f): |
92 | | - x = BookScanner(f) |
93 | | - x() |
94 | | - |
95 | | - |
96 | | -if __name__ == '__main__': |
97 | | - input_list = ["Test/a_example.txt", |
98 | | - "Test/b_read_on.txt", |
99 | | - "Test/c_incunabula.txt", |
100 | | - "Test/e_so_many_books.txt", |
101 | | - "Test/f_libraries_of_the_world.txt", |
102 | | - "Test/d_tough_choices.txt"] |
103 | | - |
104 | | - from multiprocessing import Pool |
105 | | - |
106 | | - pool = Pool(4) |
107 | | - for file in input_list: |
108 | | - pool.apply_async(run_it, (file,)) |
109 | | - pool.close() |
110 | | - pool.join() |
| 1 | +class BookScanner: |
| 2 | + def __init__(self, file): |
| 3 | + self.file = file |
| 4 | + f = open(self.file, "r") |
| 5 | + self.books, self.libraries, self.days = tuple(int(i) for i in f.readline().rstrip().split(" ")) |
| 6 | + |
| 7 | + # library_data = tuple(([], set()) for i in range(libraries)) |
| 8 | + |
| 9 | + self.library_data = {i: [] for i in range(self.libraries)} |
| 10 | + self.library_books = {i: set() for i in range(self.libraries)} |
| 11 | + self.book_scores = [int(i) for i in f.readline().rstrip().split(" ")] |
| 12 | + self.reamining_days = self.days + 10 |
| 13 | + |
| 14 | + # 扫图书馆 |
| 15 | + for i in range(self.libraries): |
| 16 | + for j in f.readline().rstrip().split(" "): |
| 17 | + self.library_data[i].append(int(j)) |
| 18 | + for j in f.readline().rstrip().split(" "): |
| 19 | + self.library_books[i].add(int(j)) |
| 20 | + f.close() |
| 21 | + |
| 22 | + self.already_sign_up = set() |
| 23 | + self.library_score_queue = [i for i in range(self.libraries)] |
| 24 | + self.result = [[self.libraries]] # + [[] for i in range(self.libraries * 2)] |
| 25 | + |
| 26 | + def operate(self): |
| 27 | + i = 0 |
| 28 | + while i < self.result[0][0]: |
| 29 | + |
| 30 | + self.library_score_queue = sorted(self.library_score_queue, |
| 31 | + key=lambda x: -sum([self.book_scores[i] for i in self.library_books[x]]) * |
| 32 | + (self.library_data[x][2] / (self.library_data[x][1] ** 2))) |
| 33 | + |
| 34 | + |
| 35 | + # self.library_score_queue = sorted(self.library_score_queue, |
| 36 | + # key=lambda x: -sum([self.book_scores[k] for k in self.library_books[x]]) / |
| 37 | + # ((self.reamining_days - self.library_data[x][1] if self.reamining_days - self.library_data[x][1] != 0 else -1) / |
| 38 | + # self.library_data[x][2])) |
| 39 | + print(self.library_score_queue) |
| 40 | + |
| 41 | + tmp = self.library_books[self.library_score_queue[0]] - self.already_sign_up |
| 42 | + if len(tmp) == 0: |
| 43 | + self.result[0][0] = self.result[0][0] - 1 |
| 44 | + continue |
| 45 | + self.result.append([]) |
| 46 | + self.result.append([]) |
| 47 | + self.result[(i + 1) * 2 - 1].append(self.library_score_queue[0]) # 图书馆编号 |
| 48 | + self.result[(i + 1) * 2 - 1].append(len(tmp)) |
| 49 | + self.result[(i + 1) * 2] = sorted(tmp, key=lambda x: -self.book_scores[x]) |
| 50 | + |
| 51 | + # 去重 |
| 52 | + self.already_sign_up = self.already_sign_up | tmp |
| 53 | + for j in self.already_sign_up: |
| 54 | + self.book_scores[j] = 0 |
| 55 | + |
| 56 | + print(self.file, i) |
| 57 | + |
| 58 | + # 判断超时 |
| 59 | + self.reamining_days -= self.library_data[self.library_score_queue[0]][1] |
| 60 | + if self.reamining_days < 0: |
| 61 | + self.result[0][0] = int((len(self.result) - 1) / 2) |
| 62 | + break |
| 63 | + |
| 64 | + i += 1 |
| 65 | + self.library_score_queue.pop(0) |
| 66 | + |
| 67 | + def write_file(self): |
| 68 | + print(self.library_data) |
| 69 | + print(self.result) |
| 70 | + output = self.result |
| 71 | + |
| 72 | + f = open(self.file.replace("Test", "Out"), "w") |
| 73 | + for i in output: |
| 74 | + f.write(" ".join([str(j) for j in i])) |
| 75 | + f.write("\n") |
| 76 | + |
| 77 | + f.close() |
| 78 | + |
| 79 | + # @staticmethod |
| 80 | + def __call__(self): |
| 81 | + # f = BookScanner(BookScanner) |
| 82 | + self.operate() |
| 83 | + self.write_file() |
| 84 | + |
| 85 | + |
| 86 | +def run_it(f): |
| 87 | + x = BookScanner(f) |
| 88 | + x() |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + input_list = ["Test/a_example.txt", |
| 93 | + "Test/b_read_on.txt", |
| 94 | + "Test/c_incunabula.txt", |
| 95 | + "Test/e_so_many_books.txt", |
| 96 | + "Test/f_libraries_of_the_world.txt", |
| 97 | + "Test/d_tough_choices.txt"] |
| 98 | + mult = True |
| 99 | + # mult = False |
| 100 | + if mult: |
| 101 | + from multiprocessing import Pool |
| 102 | + |
| 103 | + pool = Pool(4) |
| 104 | + for file in input_list: |
| 105 | + pool.apply_async(run_it, (file,)) |
| 106 | + pool.close() |
| 107 | + pool.join() |
| 108 | + else: |
| 109 | + run_it(input_list[1]) |
0 commit comments