Advertisement
TwentyEight

READQ

Feb 20th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. import numpy as np
  2. import math as m
  3. import random
  4.  
  5. # Functions
  6.  
  7. def getRandomScanD(D, T):
  8.     scanD = np.full(D, -1)
  9.     d = 0
  10.     indx = list(range(len(T)))
  11.     while True:
  12.         if len(T) < 1 or d >= len(scanD):
  13.             return scanD
  14.         n = random.randint(0, len(T)-1)
  15.         scanD[d:d+T[n]] = indx[n]
  16.         d += T[n]
  17.         T = np.delete(T, n)
  18.         indx = np.delete(indx, n)
  19.         i = T < D-d
  20.         T = T[i]
  21.         indx = indx[i]
  22.  
  23. def getYK(scanD, S, ID):
  24.     Y = np.unique(scanD)
  25.     Y = Y[ Y != -1]
  26.     K = [[0]] * len(Y)
  27.     for n,i in enumerate(Y):
  28.         K[n] = [str(x) for _,x in sorted(zip(S[list(ID[i])], ID[i]),
  29.                                      reverse = True)]
  30.     return Y, K
  31.  
  32. def rearrangeK(scanD, Y, M, K):
  33.     D = len(scanD)
  34.     nB = max([int(max(i)) for i in K])
  35.     scanB = np.full(nB+1, 0)
  36.     for ni1, Ki in enumerate(K):
  37.         nD = D - (np.argwhere(scanD == Y[ni1])[-1] + 1)
  38.         nK = nD * M[Y[ni1]]
  39.         for ni2, k in enumerate(Ki):
  40.             if ni2 > nK:
  41.                 K[ni1] = K[ni1][:nK[0]]
  42.                 break
  43.             if (scanB[int(k)] == 1):
  44.                 Ki.append(Ki.pop(Ki.index(k)))
  45.             scanB[int(k)] = 1
  46.     return K
  47.  
  48. def getScore(S, K):
  49.     s = 0
  50.     K = [i for k in K for i in k]
  51.     K = np.array(K)
  52.     K = np.unique(K)
  53.     for k in K:
  54.         s += S[int(k)]
  55.     return s
  56.  
  57. ## Read input
  58. filename = ['a_example.txt', 'b_read_on.txt', 'c_incunabula.txt',
  59.             'd_tough_choices.txt' ,'e_so_many_books.txt',
  60.             'f_libraries_of_the_world.txt']
  61. ans = ['a_ans.txt', 'b_ans.txt', 'c_ans.txt', 'd_ans.txt', 'e_ans.txt',
  62.        'f_ans.txt']
  63. inp = 2
  64.  
  65. f = open(filename[inp], 'r')
  66. B, L, D = [int(x) for x in f.readline().split()]
  67. S = np.array([int(x) for x in f.readline().split()])
  68. N, T, M = np.full(L, 0), np.full(L, 0), np.full(L, 0)
  69. ID = np.full(L, set())
  70. for i in range(L):
  71.     N[i], T[i], M[i] = [int(x) for x in f.readline().split()]
  72.     ID[i] = set([int(x) for x in f.readline().split()])
  73. f.close()
  74.  
  75. numIter = 100
  76. count = numIter
  77. maxScore = 0
  78. while (count):
  79.     scanD = getRandomScanD(D, T.copy())
  80.     Y, K = getYK(scanD, S, ID)
  81.     K = rearrangeK(scanD, Y, M, K)
  82.     s = getScore(S, K)
  83.     count -= 1
  84.     print(f'{count} {s}')
  85.     if (s > maxScore):
  86.         print('^^^^^')
  87.         maxScore = s
  88.         maxY = Y.copy()
  89.         maxK = K.copy()
  90.         count = numIter
  91.  
  92. Y = maxY
  93. K = maxK
  94. f2 = open(ans[inp], 'w')
  95. f2.write(f'{len(Y)}\n')
  96. for n,i in enumerate(Y):
  97.     f2.write(f'{i} {len(K[n])}\n')
  98.     f2.write(f"{' '.join(K[n])}\n")
  99. f2.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement