Given N tasks and M people, where each task has a start time and end time, implement a greedy algorithm to find the maximum number of tasks that can be completed by M people. Each task can only be completed by one person and a person can only be assigned one task at a time. Two tasks cannot be completed simultaneously by the same person. Input The input consists of two integers N and M (1 ≤ N, M≤ 10^3), the number of activities and the number of people, respectively. This is followed by N lines representing the activities. Each line contains two integers S₁ and E₁ (0 ≤ S; ≤ E: ≤ 10^9), representing the start and end times of the activity, respectively. Output Output a single integer representing the maximum number of activities that can be completed.

icon
Related questions
Question

Please use python and python file i/o to solve the problem. Create an input file input2_1.txt as shown below, solve the problem and print the output to output2_1.txt file as shown in the question.

Sample Input 1
52
15
36
25
8 10
69
Sample Input 2
5 2
14
25
67
48
36
Sample Input 3
52
1 10
2 10
67
48
36
Sample Input 4
83
57
24
68
8 10
1 3
79
35
26
Sample Output 1
4
Sample Output 2
4
Sample Output 3
3
Sample Output 4.
8
Transcribed Image Text:Sample Input 1 52 15 36 25 8 10 69 Sample Input 2 5 2 14 25 67 48 36 Sample Input 3 52 1 10 2 10 67 48 36 Sample Input 4 83 57 24 68 8 10 1 3 79 35 26 Sample Output 1 4 Sample Output 2 4 Sample Output 3 3 Sample Output 4. 8
Given N tasks and M people, where each task has a start time and
end time, implement a greedy algorithm to find the maximum
number of tasks that can be completed by M people.
Each task can only be completed by one person and a person can
only be assigned one task at a time. Two tasks cannot be
completed simultaneously by the same person.
Input
The input consists of two integers N and M (1 ≤ N, M≤ 10^3),
the number of activities and the number of people, respectively.
This is followed by N lines representing the activities. Each
line contains two integers S₁ and E₁ (0 ≤ S₁ ≤ E₁ ≤ 10^9),
representing the start and end times of the activity,
respectively.
Output
Output a single integer representing the maximum number of
activities that can be completed.
Transcribed Image Text:Given N tasks and M people, where each task has a start time and end time, implement a greedy algorithm to find the maximum number of tasks that can be completed by M people. Each task can only be completed by one person and a person can only be assigned one task at a time. Two tasks cannot be completed simultaneously by the same person. Input The input consists of two integers N and M (1 ≤ N, M≤ 10^3), the number of activities and the number of people, respectively. This is followed by N lines representing the activities. Each line contains two integers S₁ and E₁ (0 ≤ S₁ ≤ E₁ ≤ 10^9), representing the start and end times of the activity, respectively. Output Output a single integer representing the maximum number of activities that can be completed.
Expert Solution
steps

Step by step

Solved in 3 steps with 3 images

Blurred answer