#C4562. Activity Selection Problem
Activity Selection Problem
Activity Selection Problem
You are given N activities with their start times and end times. Your task is to select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time.
Note: Two activities are considered non-overlapping if the start time of an activity is not less than the end time of the previously selected activity. The problem can be approached using a greedy algorithm where the selection is based on the earliest finishing times.
The problem can be mathematically described as follows: Given two sequences, \( S = [s_1, s_2, \dots, s_N] \) and \( E = [e_1, e_2, \dots, e_N] \), where \( s_i \) and \( e_i \) denote the start and end times of the \( i^\text{th} \) activity respectively, select a subset of activities such that no two selected activities overlap and the size of this subset is maximized. Formally, if the selected indices are \( i_1, i_2, \dots, i_k \) (with the activities sorted by end time), then we require: \[ s_{i_{j+1}} \ge e_{i_j} \quad \text{for } 1 \le j < k, \] with the objective to maximize \( k \).
Example:
Input: 6 1 3 0 5 8 5 2 4 6 7 9 9</p>Output: 4
inputFormat
The input is read from stdin and consists of three lines:
- The first line contains an integer N (\(1 \le N \le 10^5\)), representing the number of activities.
- The second line contains N integers separated by spaces, representing the start times of the activities.
- The third line contains N integers separated by spaces, representing the end times of the activities.
outputFormat
Output a single integer to stdout representing the maximum number of non-overlapping activities that can be performed.
## sample6
1 3 0 5 8 5
2 4 6 7 9 9
4