SabirSazzad

greedy activity

Oct 31st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct Set{
  4.     int start;
  5.     int end;
  6. };
  7. void greedyActivity(Set s[], int siz)
  8. {
  9.     cout << "(1) " << "[" << s[0].start << "," << s[0].end << "]" <<endl;
  10.     int nxt = s[0].end;
  11.     for(int i=0; i<siz; i++)
  12.     {
  13.         if(nxt<=s[i].start)
  14.         {
  15.             cout << "(" << (i+1) << ") " << "[" << s[i].start << "," << s[i].end << "]" <<endl;
  16.             nxt = s[i].end;
  17.         }
  18.  
  19.     }
  20. }
  21. int main()
  22. {
  23.     Set s[11] = {0};
  24.     int start[11] = {1,3,0,5,3,5,6,8,8,2,12};
  25.     int finish[11] = {4,5,6,7,8,9,10,11,12,13,14};
  26.     int i,j;
  27.     //user input.....
  28.     for(i=0; i<11; i++)
  29.     {
  30.         s[i].start = start[i];
  31.         s[i].end = finish[i];
  32.     }
  33.     //sort according to ending time
  34.     for(i=1;i<11;++i)
  35.     {
  36.         for(j=0;j<(11-i);++j)
  37.         {
  38.             if(s[j].end>s[j+1].end)
  39.             {
  40.                 swap(s[j],s[j+1]);
  41.             }
  42.         }
  43.     }
  44.     greedyActivity(s,11);
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment