Advertisement
Alhiris

Untitled

Feb 20th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. import math
  2.  
  3. with open("input.txt","r") as f:
  4. inp=f.readlines()
  5.  
  6. x=inp[0]
  7. x=x.replace("\n","")
  8. x=x.split()
  9. BooksNrGlobal=int(x[0])
  10. LibNrGlobal=int(x[1])
  11. TotalDaysGlobal=int(x[2])
  12. inp.remove(inp[0])
  13.  
  14. class Library:
  15. SignUp=0
  16. NumberBooks=0
  17. BooksPerDay=0
  18. Index=0
  19. AverageValue=0
  20. ListOfBooks=[]
  21.  
  22. def __init__(self, libbooks,libsignup,booksperday,numberoflibbooks,ind):
  23. self.SignUp=libsignup
  24. self.BooksPerDay=booksperday
  25. self.NumberBooks=numberoflibbooks
  26. self.ListOfBooks=libbooks
  27. self.Index=ind
  28.  
  29.  
  30. x=inp[0]
  31. x=x.replace("\n","")
  32. x=x.split()
  33. BooksGlobal=[]
  34. for i in range(len(x)):
  35. BooksGlobal.append((i,int(x[i])))
  36. inp.remove(inp[0])
  37. FreqGlobalBooks=[1]*(1+BooksNrGlobal)
  38.  
  39. Libraries=[]
  40.  
  41. for i in range(0,len(inp)-1,2):
  42. x=inp[i]
  43. x=x.replace("\n","")
  44. x=x.split()
  45. libbooksnr=int(x[0])
  46. libsignup=int(x[1])
  47. booksperday=int(x[2])
  48. x=inp[i+1]
  49. x=x.replace("\n","")
  50. x=x.split()
  51. libbooks=[]
  52. for j in range(libbooksnr):
  53. if BooksGlobal[int(x[j])] not in libbooks:
  54. libbooks.append(BooksGlobal[int(x[j])])
  55. libbooks=sorted(libbooks,key=lambda el:el[1])
  56. Libraries.append(Library(libbooks,libsignup,booksperday,libbooksnr,i//2))
  57.  
  58. Solution = []
  59. cnt=0
  60. usedDays=0
  61. while usedDays<TotalDaysGlobal and cnt<LibNrGlobal:
  62. maxi=-1
  63. elem=0
  64. nrelem=0
  65. for x in Libraries:
  66. i=0
  67.  
  68. x.AverageValue=0
  69. n=x.NumberBooks
  70. while( i<n ):
  71. if FreqGlobalBooks[x.ListOfBooks[i][0]]==0:
  72. x.ListOfBooks.pop(i)
  73. n-=1
  74. i+=1
  75.  
  76. if(x.SignUp+n//x.BooksPerDay>TotalDaysGlobal-usedDays-1):
  77. n=((TotalDaysGlobal-usedDays-1)-x.SignUp)*x.BooksPerDay
  78. for j in range(n):
  79. x.AverageValue += x.ListOfBooks[j][1]
  80.  
  81. x.AverageValue/=(x.SignUp+(n/x.BooksPerDay))
  82.  
  83. if(x.AverageValue>maxi):
  84. maxi=x.AverageValue
  85. elem=x
  86. nrelem=n
  87. cnt+=1
  88. usedDays+=elem.SignUp
  89. lst=[]
  90. for j in range(nrelem):
  91. FreqGlobalBooks[elem.ListOfBooks[j][0]]=0
  92. lst.append(elem.ListOfBooks[j][0])
  93.  
  94. Solution.append((elem.Index,n,lst))
  95. Libraries.remove(elem)
  96.  
  97. with open("output.txt","w") as cout:
  98.  
  99. cout.write(str(len(Solution))+'\n')
  100. for x in Solution:
  101. cout.write(str(x[0])+' '+str(x[1])+'\n')
  102. for y in x[2]:
  103. cout.write(str(y)+' ')
  104. cout.write('\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement