Advertisement
veryinnocentboy

Untitled

May 24th, 2021
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1.  
  2. '''
  3. HURRY !!!!    loooda
  4.  
  5. backtracking
  6. and recursion
  7. '''
  8.  
  9.  
  10. maxTasksCount=0
  11. def swap(i, j):
  12.     t = tasks[i]
  13.     tasks[i] = tasks[j]
  14.     tasks[j] = t
  15. def howManyTasks(startMoney, tasks):
  16.     count  = 0
  17.     currentPlace = 0
  18.     for i in range(len(tasks)):
  19.         currentPenalty = 2*(abs(tasks[i][0]-currentPlace)) + tasks[i][1]
  20.         if startMoney >= currentPenalty:
  21.             count = count + 1
  22.             startMoney = startMoney - currentPenalty
  23.             currentPlace = tasks[i][0]
  24.         else :
  25.             return count
  26. def fun(k, l):
  27.     global maxTasksCount
  28.     if l == 0:
  29.         count = howManyTasks(startMoney, tasks)
  30.         print(tasks, count)
  31.         maxTasksCount = max(maxTasksCount, count)
  32.         return
  33.     for i in range(k, len(tasks)):
  34.         swap(k, i)
  35.         fun(k+1, l-1)
  36.         swap(k, i)
  37.  
  38.  
  39. startMoney = 11
  40. tasks = [[3,11], [2,1], [5, 2]]
  41. fun(0, len(tasks))
  42. print(maxTasksCount)
  43. startMoney = 15
  44. tasks = [[3,11], [2,1], [5, 2]]
  45. fun(0, len(tasks))
  46. print(maxTasksCount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement