Movie Festival

Key Idea: Greedy interval scheduling: sort by end time and take a movie whenever it starts after the last one ends.

Solution

#include <bits/stdc++.h>
using namespace std;
//author: von_Braun
#define ll long long
#define lli long long int
#define pb push_back
#define rep(var, start, num) for(ulli var = start; var <start + num; var++)
#define all(x) x.begin(), x.end()
#define ulli unsigned long long int
#define ull unsigned long long
bool sortbysec(const pair<ll,ll> &a,const pair<ll,ll> &b) { return (a.second < b.second); }

void solve() {
    int n;
    cin>>n;
    vector<pair<int,int>> mov(n);
    rep(i,0,n) {
        cin>>mov[i].second>>mov[i].first;
    }
    sort(all(mov));
    int cnt{0};
    int etime{-1};
    rep(i,0,n) {
        if (mov[i].second >= etime) {cnt++; etime=mov[i].first;}
    }
    cout<<cnt<<endl;
}

int main() {
    //add quotes incase input output file
    //freopen(input.txt,r,stdin);
    //freopen(output.txt,w,stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tc = 1;
    // cin >> tc;
    for (int t = 1; t <= tc; t++) {
        solve();
    }
}