-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum_common_points_set.cpp
56 lines (55 loc) · 1.01 KB
/
minimum_common_points_set.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
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<pair<int,int>> v;
for(int i=0;i<n;i++)
{
int a,b;
cin>>a>>b;
v.push_back(make_pair(a,b));
}
sort(v.begin(),v.end());
pair<int,int> range;
vector<int> ans;
range=make_pair(v[0].first,v[0].second);
for(int i=1;i<n;i++)
{
if(range.second<v[i].first || range.first>v[i].second)
{
ans.push_back(range.first);
range.first=v[i].first;
range.second=v[i].second;
if(i==n-1)
{
ans.push_back(range.first);
}
continue;
}
else if(range.first<=v[i].first && range.second>=v[i].second)
{
range.first=v[i].first;
range.second=v[i].second;
}
else if(range.first<=v[i].first && range.second<=v[i].second)
{
range.first=v[i].first;
}
else if(range.first>=v[i].first && range.second>=v[i].second)
{
range.second=v[i].second;
}
if(i==n-1)
{
ans.push_back(range.first);
}
}
cout<<ans.size()<<"\n";
for(int i=0;i<ans.size();i++)
{
cout<<ans[i]<<" ";
}
return 0;
}