Advertisement
monyca98

problema spectacolelor greedy

May 30th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. int n,m,s[20];
  5. struct activ
  6. {   int x,y,z;
  7. };
  8. activ a[20],aux;
  9. void citire(int &n)
  10. {   ifstream f("e:\\info\\greedy\\spectacol.txt");
  11.     f>>n;
  12.     for(int i=1;i<=n;i++)
  13.         f>>a[i].x>>a[i].y>>a[i].z;
  14.     f.close();
  15. }
  16. void afisare()
  17. {   for(int i=1;i<=n;i++)
  18.     {   cout<<a[i].x<<" "<<a[i].y<<" "<<a[i].z;
  19.         cout<<endl;
  20.     }
  21. }
  22. void sortare()
  23. {   activ aux;
  24.     for(int i=1;i<n;i++)
  25.         for(int j=i+1;j<=n;j++)
  26.             if(a[i].z>a[j].z)
  27.             {   aux=a[i];
  28.                 a[i]=a[j];
  29.                 a[j]=aux;
  30.             }
  31. }
  32. void greedy(int &m)
  33. {   int j=1;s[1]=1;
  34.     for(int i=2;i<=n;i++)
  35.         if(a[i].y>=a[s[j]].z)
  36.         {   j++;
  37.             s[j]=i;
  38.         }
  39.     m=j;
  40. }
  41. void afis()
  42. {   for(int i=1;i<=m;i++)
  43.         cout<<"activitatea "<<a[s[i]].x<<" ora de inceput "<<a[s[i]].y<<" ora de sfarsit "<<a[s[i]].z<<endl;
  44. }
  45. int main()
  46. {   citire(n);
  47.     afisare();
  48.     cout<<endl;
  49.     sortare();
  50.     afisare();
  51.     cout<<endl;
  52.     greedy(m);
  53.     afis();
  54.  
  55. }
  56. 8
  57. 1 9 11
  58. 2 12 13
  59. 3 8 10
  60. 4 10 12
  61. 5 16 18
  62. 6 14 16
  63. 7 20 22
  64. 8 19 21
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement