Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Algo Lab #3 (Greedy algo_Practise)
- #include<cstdio>
- #include<algorithm>
- using namespace std;
- #define SIZE 101
- //int S[SIZE], E[SIZE];
- struct Activity
- {
- int startT, endT;
- };
- Activity A[SIZE];
- bool operator<(const Activity &lhs, const Activity &rhs)
- {
- return lhs.endT < rhs.endT;
- }
- int activity_selection(int n)
- {
- sort(&A[0], &A[n]);
- int c = 0, lastEnd = 0;
- for (int i=0; i<n; i++){
- if (A[i].startT >= lastEnd ){
- c++;
- lastEnd = A[i].endT;
- printf("[%d, %d]\n", A[i].startT, A[i].endT);
- }
- }
- return c;
- }
- int main()
- {
- freopen("input.txt", "r", stdin);
- int n;
- scanf("%d", &n);
- for(int i = 0; i < n; ++i)
- scanf("%d %d", &A[i].startT,
- &A[i].endT);
- printf("Input data:\n\n");
- for(int i = 0; i < n; ++i)
- printf("[%d, %d]\n", A[i].startT, A[i].endT);
- int c = activity_selection(n);
- printf("\nSorted Data:\n\n");
- for(int i = 0; i < n; ++i)
- printf("[%d, %d]\n", A[i].startT, A[i].endT);
- printf("\nMax activities selected = %d\n", c);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement