Pabon_SEC

Activity Selection

Oct 16th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3.  
  4. using namespace std;
  5.  
  6. struct data
  7. {
  8.     int start,nd,index;
  9.  
  10. }arr[1000];
  11.  
  12. bool cmp(data lhs,data rhs)
  13. {
  14.     return lhs.nd<rhs.nd;
  15. }
  16.  
  17. int main()
  18. {
  19.     int koyta_activity,i,index,counter,nd_time;
  20.  
  21.     while(cin>>koyta_activity)
  22.     {
  23.  
  24.         for(i=1;i<=koyta_activity;++i)
  25.         {
  26.             cin>>arr[i].start>>arr[i].nd;
  27.  
  28.             arr[i].index=i;
  29.         }
  30.  
  31.         sort(arr+1,arr+koyta_activity+1,cmp);
  32.  
  33.         nd_time=0;
  34.  
  35.         counter=0;
  36.  
  37.         cout<<endl;
  38.         cout<<endl;
  39.  
  40.         for(i=1;i<=koyta_activity;++i)
  41.         {
  42.             if(nd_time<=arr[i].start)
  43.             {
  44.                 cout<<arr[i].index<<" --> "<<arr[i].start<<" "<<arr[i].nd<<endl;
  45.  
  46.                 nd_time=arr[i].nd;
  47.  
  48.                 counter++;
  49.             }
  50.         }
  51.  
  52.         cout<<endl;
  53.  
  54.         cout<<"Total number of selected activities is "<<counter<<endl<<endl;
  55.     }
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment