Advertisement
Tavxela

scheduling problem

Jun 12th, 2021
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1.  
  2. #include<iostream>
  3. #include<algorithm>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class Job
  9. {
  10. public:
  11.     string title;
  12.     int deadline;
  13.     int profit;
  14. };
  15.  
  16. bool comparison(Job a, Job b)
  17. {
  18.     return (a.profit > b.profit);
  19. }
  20.  
  21. void print_job_scheduling(vector<Job> jobs)
  22. {
  23.  
  24.     sort(jobs.begin(), jobs.end(), comparison);
  25.  
  26.     int result[jobs.size()];
  27.     bool slot[jobs.size()];
  28.  
  29.     for (int i = 0; i < jobs.size(); i++)
  30.         slot[i] = false;
  31.  
  32.     for (int i = 0; i < jobs.size(); i++)
  33.     {
  34.         for (int j = min(int(jobs.size()), jobs[i].deadline) - 1; j >= 0; j--)
  35.         {
  36.             if (slot[j]==false)
  37.             {
  38.                 result[j] = i;
  39.                 slot[j] = true;
  40.                 break;
  41.             }
  42.         }
  43.     }
  44.  
  45.     for (int i=0; i < jobs.size(); i++)
  46.     if (slot[i])
  47.         cout << jobs[result[i]].title << " ";
  48. }
  49.  
  50. int main()
  51. {
  52.  
  53.     print_job_scheduling({
  54.         {"Job 1", 2, 100},
  55.         {"job 2", 1, 19},
  56.         {"Job 3", 2, 27},
  57.         {"job 4", 4, 25},
  58.         {"job 5", 3, 15}
  59.     });
  60.     return 0;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement