CSES - Shared codeLink to this code: https://cses.fi/paste/c3b2a6ef0510a27812d5e2/
#include <bits/stdc++.h>
#define mod 1000000007;
#define ll long long
using namespace std;
bool comp(pair<ll, ll> &a, pair<ll, ll> &b)
{
return a.second < b.second;
}
int main()
{
int n;
ll a, b;
int index = 1;
cin >> n;
vector<pair<ll, ll>> movie_times;
for (int i = 0; i < n; ++i)
{
cin >> a >> b;
movie_times.push_back({a, b});
}
sort(movie_times.begin(), movie_times.end(), comp);
int end_time = movie_times[0].second;
int start_time = 0;
int movies_watched = 1;
int next = 1, prev = 0;
while (next <= n - 1 && prev <= n - 1)
{
if (movie_times[prev].second <= movie_times[next].first)
{
movies_watched++;
next++;
prev++;
}
else
{
next++;
}
}
cout << movies_watched;
return 0;
}