The streamer hopes to find out the peak number of viewers online simultaneously during the livestream. So, he sought your help as a programmer.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line contains N, which represents that N individuals are coming up next. Following that, each line contains two integers, Ai and Bi, representing the time when this person enters and leaves the live stream, respectively. At each time point, only one person will enter or leave. | Please output the peak number of viewers online simultaneously during the live stream. |
3 1 3 5 6 2 4 | 2 |
Thought Process
Store the "entry times" and "exit times" in separate arrays and sort them.
Run a For loop for the entry times, incrementing the count of viewers each time. Inside, run another For loop for the exit times. Maintain a Start variable (initialized to 0) to start checking from that position. If the exit time is less than the current entry time, decrease the count of viewers until the exit time is greater than or equal to the entry time. Then, break the exit time loop. Finally, track the maximum number of viewers at any given time and output this value.
Sample Code-ZeroJudge F731: Streaming Room
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vector<int>in;
vector<int>out;
for (int i = 0; i<N; i++)
{
int a, b;
cin >> a >> b;
in.push_back(a);
out.push_back(b);
}
sort(in.begin(), in.end());
sort(out.begin(), out.end());
int people = 0, start = 0, max = -999;
for (int i = 0; i<N; i++)
{
people++;
for (int j = start; j<N; j++)
{
if (out[j] > in[i])
{
start = j;
break;
}
people--;
}
if (people > max) max = people;
}
cout << max << "\n";
}
//ZeroJudge F731
//Dr. SeanXD