-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem.py
executable file
·160 lines (110 loc) · 4.67 KB
/
problem.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
from tqdm import trange, tqdm
inputs = ['a_example', 'b_read_on', 'c_incunabula', 'd_tough_choices', 'e_so_many_books', 'f_libraries_of_the_world']
class Problem:
def __init__(self):
pass
def _prepare_input(self, in_filename):
with open('input/' + in_filename, 'r') as in_file:
self.B, self.L, self.D = map(int, in_file.readline().split())
self.S = list(map(int, in_file.readline().rstrip().split()))
self.LIBS = []
for j in range(self.L):
lib = dict()
lib['n'], lib['t'], lib['m'] = list(map(int, in_file.readline().rstrip().split()))
lib['b'] = list(map(int, in_file.readline().rstrip().split()))
self.LIBS.append(lib)
def _generate_output(self, out_filename):
with open('output/' + out_filename, 'w') as out_file:
out_file.write(str(len(self.selected_libs)) + '\n')
for lib in self.selected_libs:
out_file.write(str(lib['i']) + ' ' + str(len(lib['b'])) + '\n')
out_file.write(' '.join(map(str, lib['b'])) + '\n')
def _lib_best_scored_books(self, idx, days):
scores = { b: self.S[b] for b in self.LIBS_DICT[idx]['b'] }
m = self.LIBS_DICT[idx]['m']
score = 0
books = []
nb_books_can_scan = days * m
for b in sorted(scores, key=scores.get, reverse=True):
if b not in self.excluded_books and nb_books_can_scan > 0:
books.append(b)
nb_books_can_scan -= 1
score += scores[b]
return (score, books)
def _best_scored_lib(self, days):
best_lib_idx = -1
best_lib_score = -1
best_lib_books = None
for i in tqdm(self.LIBS_DICT):
days_left_after_signup = days - self.LIBS_DICT[i]['t']
if days_left_after_signup > 0:
score, books = self._lib_best_scored_books(i, days_left_after_signup)
if self.idx == 5:
score = (len(books) * score) / self.LIBS_DICT[i]['t']
else:
score /= self.LIBS_DICT[i]['t']
if score > best_lib_score:
best_lib_score, best_lib_idx, best_lib_books = score, i, books
return (best_lib_idx, best_lib_books)
def _work(self):
days_left = self.D
self.selected_libs = []
self.excluded_books = set()
self.LIBS_DICT = { i: self.LIBS[i] for i in range(len(self.LIBS)) }
while True:
l_idx, books = self._best_scored_lib(days_left)
if l_idx == -1 or days_left <= 0:
break
days_left -= self.LIBS_DICT[l_idx]['t']
lib = {'i': l_idx, 'b': books}
self.selected_libs.append(lib)
self.excluded_books.update(books)
del self.LIBS_DICT[l_idx]
def test(self, idx):
self.idx = idx
self._prepare_input(inputs[idx] + '.txt')
self._work()
self._generate_output(inputs[idx] + '.test')
score = self._score(inputs[idx] + '.test')
print('test %s score is : %d' % (chr(97+idx), score))
def solve(self):
for idx in range(len(inputs)):
self.idx = idx
self._prepare_input(inputs[idx] + '.txt')
self._work()
self._generate_output(inputs[idx] + '.out')
self.scores()
def _score(self, out_filename):
# TODO: verify books for days inconsistencies
score = 0
l, libs = None, []
with open('output/' + out_filename, 'r') as out_file:
l = int(out_file.readline())
for i in range(l):
lib = dict()
lib['i'], lib['b'] = map(int, out_file.readline().rstrip().split())
lib['books'] = list(map(int, out_file.readline().rstrip().split()))
libs.append(lib)
for i in range(l):
for j in range(libs[i]['b']):
book_idx = libs[i]['books'][j]
score += self.S[book_idx]
return score
def scores(self):
scores = []
with open('output/scores', 'w') as scores_file:
for idx in range(len(inputs)):
self._prepare_input(inputs[idx] + '.txt')
score = self._score(inputs[idx] + '.out')
scores.append(score)
scores_file.write('\n'.join(chr(97 + idx) + ' : ' + str(scores[idx]) for idx in range(len(scores))))
scores_file.write('\n\n')
scores_file.write('s: %d' % sum(scores))
def main():
p = Problem()
p.test(5)
# p.scores()
# p.solve()
if __name__ == '__main__':
main()