Advertisement
Eternoseeker

S-Ass2-JobSchedule

Nov 24th, 2023
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | Source Code | 0 0
  1. #4 sep
  2. if __name__ == "__main__":
  3.     n = int(input("Enter the number of jobs: "))
  4.     jobs = []
  5.     print("Enter profit and deadline (space-separated) for each job:")
  6.     for i in range(n):
  7.         profit, deadline = map(int, input().split())  # Read profit and deadline as space-separated inputs
  8.         jobs.append((profit, deadline, i + 1))
  9.  
  10.     jobs = sorted(jobs, key=lambda x: x[0], reverse=True)
  11.     max_deadline = max(jobs, key=lambda x: x[1])[1]
  12.  
  13.     sz = [0] * max_deadline  # Initialize a list to store the job schedule
  14.     total_profit = 0
  15.  
  16.     for i in range(n):
  17.         current_deadline = jobs[i][1]
  18.         while current_deadline > 0:
  19.             if sz[current_deadline - 1] == 0:
  20.                 sz[current_deadline - 1] = jobs[i][2]
  21.                 total_profit += jobs[i][0]
  22.                 break
  23.             current_deadline -= 1
  24.  
  25.     print("Maximum profit:", total_profit)
  26.     print("Job schedule:", sz)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement