-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 84f5d2a
Showing
4 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data/* | ||
out/* | ||
*.zip | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
import operator | ||
|
||
class Library(object): | ||
def __init__(self, idx, nBook, nSign, nScan, books): | ||
self.id = idx | ||
self.nBook = nBook | ||
self.nSign = nSign | ||
self.nScan = nScan | ||
self.books = list(reversed(sorted(books, key=operator.attrgetter('score')) )) | ||
self.registered = False | ||
self.registeredDay = None | ||
|
||
def predMaxScore(self, nDays): | ||
restDays = nDays - self.nSign | ||
if restDays < 0: | ||
return 0 | ||
sumScore = 0 | ||
bookCap = restDays*self.nScan | ||
readBooks= self.books[:bookCap] | ||
for book in readBooks: | ||
sumScore += book.score | ||
return sumScore | ||
|
||
def getSol(self, nDays): | ||
restDays = nDays - self.nSign | ||
if restDays < 0: | ||
return 0 | ||
bookCap = restDays*self.nScan | ||
readBooksId=[book.id for book in self.books[:bookCap]] | ||
return readBooksId | ||
|
||
def __repr__(self): | ||
return "Lib[{}:{}:{}--{}]".format(self.nBook, self.nSign, self.nScan, self.books) | ||
|
||
|
||
class Book(object): | ||
def __init__(self, id, score, scanned=False): | ||
self.id = id | ||
self.score = score | ||
|
||
def __repr__(self): | ||
return "Book[{}:{}]".format(self.id, self.score) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
|
||
from library import Library, Book | ||
import numpy as np | ||
|
||
class Problem(object): | ||
def __init__(self, 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(' '))] | ||
self.book2Score = {book.id: int(book.score) for book in self.books} | ||
self.libs = [] | ||
for i 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(i, nBooks, nSign, nScan, books) | ||
self.libs.append(lib) | ||
self.pri() | ||
|
||
def solve(self): | ||
t = 0 | ||
solution = [] | ||
readBookSet = set() | ||
while t < self.nDays: | ||
scoreList = [] | ||
for lib in self.libs: | ||
if lib.registered == False: | ||
scoreList.append( | ||
lib.predMaxScore(self.nDays - t) | ||
) | ||
else: | ||
scoreList.append(0) | ||
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 | ||
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.getSol(self.nDays) | ||
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') | ||
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) | ||
#p = Problem('data/b.txt') | ||
#p = Problem('data/c.txt') | ||
#p = Problem('data/d.txt') | ||
|
||
#solve() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
python3 main.py | ||
python3 main.py | ||
python3 main.py | ||
python3 main.py | ||
python3 main.py |