2 seconds
256 megabytes
standard input
standard output
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Print a single number — the number of vertexes in the maximum clique of the given graph.
4
2 3
3 1
6 1
0 2
3 题目意思:
n个点,每个点有x和w,若两个点i和j满足|xi-xj|>=wi+wj,则两个点连边,求最大团。
思路:
每个点可以构造线段[xi-wi,xi+wi],若两个线段不想交则满足不等式,那么问题转换为n条线段,求最大的集合使得线段两两不想交,按线段右端点从小到大排序贪心。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 200005 struct node{
int l, r;
}a[N]; bool cmp(node a,node b){
if(a.r==b.r) return a.l>b.l;
return a.r<b.r;
} int n; main()
{
int i, j, k;
cin>>n;
for(i=;i<n;i++){
scanf("%d %d",&j,&k);
a[i].l=j-k;
a[i].r=j+k;
}
sort(a,a+n,cmp);
int ans=;j=a[].r;
for(i=;i<n;i++){
if(a[i].l>=j){
ans++;
j=a[i].r;
}
}
printf("%d\n",ans);
}