-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (96 loc) · 3.39 KB
/
main.py
File metadata and controls
118 lines (96 loc) · 3.39 KB
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
from library import Library, Book
import numpy as np
class Problem(object):
def __init__(self, filename):
print('file--', filename)
f = open(filename)
l = f.readline().split(' ')
self.filename = filename
self.nBooks, self.nLibs, self.nDays = int(l[0]), int(l[1]), int(l[2])
self.books = [ Book(i,int(score)) for i, score in enumerate(f.readline().split(' '))]
sumb = 0
for b in self.books:
sumb += b.score
print( sumb/1000000 )
self.book2Score = {book.id: int(book.score) for book in self.books}
self.libs = []
for libId in range(self.nLibs):
l = [int(i) for i in f.readline().split(' ')]
nBooks, nSign, nScan = l[0], l[1], l[2]
books = [ Book(int(i), int(self.book2Score[int(i)])) for i in f.readline().split(' ') ]
lib = Library(libId, nBooks, nSign, nScan, books)
self.libs.append(lib)
self.pri()
def solve(self):
t = 0
solution = []
readBookSet = set()
while t < self.nDays:
print('-----', t)
scoreList = []
readBookList = []
for lib in self.libs:
if lib.registered == False:
score, curBookList = lib.predMaxScore(self.nDays - t, readBookSet)
scoreList.append( score )
readBookList.append( curBookList )
else:
scoreList.append(0)
readBookList.append( [ ] )
if len(scoreList) == 0:
break
if max(scoreList) == 0:
break
libIndex = scoreList.index(max(scoreList))
self.libs[libIndex].registeredDay = t
self.libs[libIndex].registered = True
self.libs[libIndex].solBooks = readBookList[libIndex]
readBookSet = readBookSet.union(readBookList[libIndex])
solution.append(self.libs[libIndex])
t += self.libs[libIndex].nSign
print([(lib.id, lib.nSign, lib.registeredDay) for lib in solution])
return solution
def dump(self, solution):
f = open(self.filename.replace('data/', 'out/'), 'w+')
f.write('{}\n'.format(len(solution)))
for lib in solution:
books = lib.solBooks
f.write('{} {}\n'.format(
lib.id, len(books)
))
s = ""
for book in books:
s+= str(book) + ' '
s+='\n'
f.write(s)
f.close()
def pri(self):
print('------')
print(self.nBooks, self.nLibs, self.nDays)
#print(self.libs)
print('------')
if __name__ == "__main__":
# p = Problem('data/a.txt')
# p = Problem('data/b.txt')
# p = Problem('data/c.txt')
# p = Problem('data/d.txt')
# p = Problem('data/e.txt')
# p = Problem('data/f.txt')
# p = Problem('data/a.txt')
# solution = p.solve()
# p.dump(solution)
# p = Problem('data/b.txt')
# solution = p.solve()
# p.dump(solution)
# p = Problem('data/c.txt')
# solution = p.solve()
# p.dump(solution)
p = Problem('data/d.txt')
solution = p.solve()
p.dump(solution)
# p = Problem('data/e.txt')
# solution = p.solve()
# p.dump(solution)
# p = Problem('data/f.txt')
# solution = p.solve()
# p.dump(solution)