-
Notifications
You must be signed in to change notification settings - Fork 0
/
1112.cpp
62 lines (59 loc) · 1.9 KB
/
1112.cpp
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
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;
void solve(){
int n; cin>>n;
int a[n + 2],b[n + 2];
for(int i = 0;i < n;++i){
cin>>a[i]>>b[i];
if(a[i] > b[i]){
swap(a[i],b[i]);
}
}
for(int i = 0;i < n;++i){
for(int j = 0;j < i;++j){
if(a[j] > a[i]){
swap(a[i],a[j]);
swap(b[i],b[j]);
} else if(a[j] == a[i] && b[j] > b[i]){
swap(a[i],a[j]);
swap(b[i],b[j]);
}
}
}
int dp[n + 2];
int prev[n + 2];
for(int i = 0;i < n;++i){
dp[i] = 1;
prev[i] = -1;
}
int mx = -1;
int res = -1;
for(int i = 0;i < n;++i){
for(int j = i - 1;j >= 0;--j){
if(b[j] <= a[i]){
if(dp[i] < dp[j] + 1){
dp[i] = dp[j] + 1;
prev[i] = j;
}
}
}
if(mx < dp[i]){
mx = dp[i];
res = i;
}
}
cout<<mx<<'\n';
int u[n + 2],v[n + 2];
int j = 0;
while(res != -1){
u[j] = a[res];
v[j] = b[res];
j++;
res = prev[res];
}
for(int i = j - 1;i >= 0;--i){
cout<<u[i]<<' '<<v[i]<<'\n';
}
int main(){
solve();
}