Advertisement
ToxicrewZeu

Greedy

Oct 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. ifstream fin("spectacole.in");
  5. ofstream fout("spectacole.out");
  6. struct interval
  7. {
  8. int x,y;
  9. };
  10. interval a[101];
  11. int n;
  12. void citeste()
  13. {
  14. fin>>n;
  15. for(int i=1;i<=n;i++)
  16. fin>>a[i].x>>a[i].y;
  17. }
  18. void Sortare()
  19. {
  20. int i,j;
  21. for(i=1;i<n;i++)
  22. for(j=i+1;j<=n;j++)
  23. if(a[i].y>a[j].y)
  24. swap(a[i],a[j]);
  25. }
  26. void Greedy()
  27. {
  28. int i,k,cnt=1;
  29. k=1;
  30. for(i=2;i<=n;i++)
  31. if(a[k].y<=a[i].x)
  32. {
  33. k=i;
  34. cnt++;
  35. }
  36. fout<<cnt;
  37. }
  38. int main()
  39. {
  40. citeste();
  41. Sortare();
  42. Greedy();
  43.  
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement