Advertisement
DevilVeNom

DAA JSD

Mar 24th, 2022
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class job{
  4.     public:
  5.         int jobid, deadline, profit;
  6. };
  7.  
  8. bool mycompare(job *x, job *y){
  9.     return x->profit > y->profit;
  10. }
  11.  
  12. int maxProfit(job **obj, int n){
  13.     int max = 0, profit = 0, i, j;
  14.     for(i = 0; i < n; i++){
  15.         if(i == 0)
  16.             max = obj[i]->deadline;
  17.         else
  18.         {
  19.             if(obj[i]->deadline > max)
  20.                 max = obj[i]->deadline;
  21.         }
  22.     }
  23.     sort(obj, obj+n, mycompare);
  24.     int store[max] = {0};
  25.     bool slot[max] = {false};
  26.    
  27.     for(i = 0; i < n; i++)
  28.     {
  29.         for(j = (obj[i]->deadline - 1); j >= 0; j--)
  30.         {
  31.             if(slot[j] == false)
  32.             {
  33.                 profit += obj[i]->profit;
  34.                 store[j] = obj[i]->jobid;
  35.                 slot[j] = true;
  36.                 break;
  37.             }
  38.         }
  39.     }
  40.     cout<<"Jobs sequence is: ";
  41.     for(i = 0; i < max; i++)
  42.     {
  43.         if(slot[i])
  44.         {
  45.             cout<<store[i]<<" ";
  46.         }
  47.     }
  48.     return profit;
  49. }
  50.  
  51. int main()
  52. {
  53.     int i, n, size, max, totalprofit = 0;
  54.     cout<<"Enter the no. of jobs: ";
  55.     cin>>n;
  56.     job *obj[n];
  57.     for(i = 0; i < n; i++)
  58.     {
  59.         obj[i] = (job*) malloc (sizeof(struct job));
  60.         cout<<"Enter jobid, deadline, and profit of job "<<i+1<<endl;
  61.         cin>>obj[i]->jobid;
  62.         cin>>obj[i]->deadline;
  63.         cin>>obj[i]->profit;
  64.     }
  65.     totalprofit = maxProfit(obj, n);
  66.     cout<<"\n Total profit is: "<<totalprofit<<"\n";
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement