Răspuns :
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
class Interval {
private:
int inf, sup;
public:
Interval(int _inf=0, int _sup=0) :
inf(_inf), sup(_sup) {};
int getInf() {
return inf;
}
int getSup() {
return sup;
}
bool in_interval(int x) {
if (inf <= x && x <= sup) return 1;
return 0;
}
bool operator< (const Interval& other) {
return this->sup < other.sup;
}
friend std::istream& operator>>(std::istream&, Interval&);
};
std::istream& operator>>(std::istream& inp, Interval& x) {
inp >> x.inf >> x.sup;
return inp;
}
int main() {
//Declarari
std::vector<Interval> v;
std::ifstream fin("titu2021.txt");
//Citire dimenisune
int n;
fin >> n;
//Caz special
if (n == 0) {
std::cout << 0;
return 0;
}
//Redimensionare vector
v.resize(n);
//Citire intervale
for (int index = 0; index < n; index++)
fin >> v[index];
//Aranjare intervale dupa capatul superior
std::sort(v.begin(), v.end());
//Selectam primul interval din lista
int sup_intv_curent_mx = v[0].getSup();
int contor_mx = 1;
//Adaugam intervale in ordinea in care apar daca acestea nu se intersecteaza cu intervalul curent maxim
for (int index = 1; index < n; index++) {
if (!v[index].in_interval(sup_intv_curent_mx)) {
sup_intv_curent_mx = v[index].getSup();
contor_mx++;
}
}
//Afisare rezultat
std::cout << contor_mx;
}