fork download
  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4.  
  5. struct Job
  6. {
  7. char id;
  8. int dead;
  9. int profit;
  10. };
  11.  
  12. bool comparison(Job a, Job b)
  13. {
  14. return (a.profit > b.profit);
  15. }
  16.  
  17. void printJobScheduling(Job arr[], int n)
  18. {
  19. sort(arr, arr+n, comparison);
  20.  
  21. int result[n];
  22. bool slot[n];
  23. for (int i=0; i<n; i++)
  24. slot[i] = false;
  25.  
  26. for (int i=0; i<n; i++)
  27. {
  28. for (int j=min(n, arr[i].dead)-1; j>=0; j--)
  29. {
  30. if (slot[j]==false)
  31. {
  32. result[j] = i;
  33. slot[j] = true;
  34. break;
  35. }
  36. }
  37. }
  38.  
  39.  
  40. for (int i=0; i<n; i++)
  41. if (slot[i])
  42. cout <<endl<< arr[result[i]].id << " ";
  43. }
  44.  
  45.  
  46. int main()
  47. {
  48. Job arr[] = { {'a', 2, 100}, {'b', 1, 19}, {'c', 3, 27},
  49. {'d', 1, 25}, {'e', 3, 15}};
  50. int n = sizeof(arr)/sizeof(arr[0]);
  51. cout << "Following is maximum profit sequence of jobs: ";
  52. printJobScheduling(arr, n);
  53. return 0;
  54. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Following is maximum profit sequence of jobs: 
d 
a 
c