-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path135_max_length_chain.cpp
More file actions
53 lines (43 loc) · 828 Bytes
/
Copy path135_max_length_chain.cpp
File metadata and controls
53 lines (43 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// https://practice.geeksforgeeks.org/problems/max-length-chain/1
#include <bits/stdc++.h>
using namespace std;
int mlc(vector< pair<int, int> > p, int n)
{
sort(p.begin(), p.end());
std::vector<int> v(n, 1);
int max_length = 1;
for (int i = 1; i < n; ++i)
{
for (int j = i; j >= 0; --j)
{
if (p[j].second < p[i].first)
{
v[i] = max(1 + v[j], v[i]);
if (v[i] > max_length)
max_length = v[i];
}
}
}
return max_length;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector< pair<int, int> > p(n);
for (int i = 0; i < n; ++i)
cin >> p[i].first >> p[i].second;
cout << mlc(p, n) << endl;
}
return 0;
}