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