D. Segments
time limit per test
memory limit per test
input
output
You are given n
Input
The first line of the input contains single integer number n (1 ≤ n ≤ 1000) — amount of segments. Following n
Output
The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any.
Examples
input
2
0 2
2 5
output
1
2
input
5
0 3
4 2
4 8
8 10
7 7
output
3
7 10 3
把所有线段按照左端点从小到大排序,放在node[]数组中,因为nod[0]木棒一定要被钉,那么现在求在node[0]木棒被钉的基础上最多能钉多少木棒。设mins = node[0].r
便利for(i = 1; i < n; i++)若node[i].l > mins那么i之前的木棒可以用一根钉子钉在一起,否则mins = min(mins, node[i].r);
以此类推.....
#include <bits/stdc++.h>
#define maxn 100005
#define MOD 1000000007
using namespace std;
typedef long long ll;
struct Node{
int l, r;
friend bool operator < (const Node&a, const Node&b){
return a.l < b.l;
}
}node[1005];
vector<int> v;
int main(){
// freopen("in.txt", "r", stdin);
int n, t = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d%d", &node[i].l, &node[i].r);
if(node[i].l > node[i].r)
swap(node[i].l, node[i].r);
}
sort(node, node+n);
int ans = 0;
int mins = node[0].r;
for(int i = 1; i < n; i++){
if(node[i].l > mins){
ans++;
v.push_back(mins);
mins = node[i].r;
}
else{
mins = min(mins, node[i].r);
}
}
v.push_back(mins);
ans++;
printf("%d\n", ans);
printf("%d", v[0]);
for(int i = 1; i < ans; i++)
printf(" %d", v[i]);
puts("");
return 0;
}