题面
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.
Input
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.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4 2 3 3 1 6 1 0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
题目链接
参考链接
题解参考:Codefroces 527D Clique Problem【思维+贪心】author:mengxiang000000
题目简述
如果一个点对(i,j)满足|xi - xj| ≥ wi + wj 则在i,j之间连边
求无向图中的最大完全子图的大小
xi-xj>=wi+wj xi-wi>=xj+wj
xj-xi>=wi+wj xj-wj>=xi+wj(与上式等效)
所以令node[i].x=X-W .node[i].y=X+W
当node[i].x>=node[j].y时,两点之间建边
有边(i,j)、(j,k)的情况下,必有边(i,j)因为node[i].y<=node[j].x<=node[j].y<=node[k].x,所以可以把k加入i,j所在图。
将所有点根据Y从小到大排序,遍历node数组,维护一个最大的max_y,当当前node[i].x>=max_y时,说明可以把当前点加入之前的最大无向完全图,node[i].y>=node[i].x>=max_y,更新max_y。
程序
#include<stdio.h>
#include<algorithm>
using namespace std;
#define maxn 200005
struct Node
{
long long x,y;
}node[maxn];
bool cmp(const Node &a,const Node &b)
{
if(a.y!=b.y)
return a.y<b.y;
else
return a.x<b.x;
}
int main()
{
int n;
long long X,W;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lld%lld",&X,&W);
node[i].x=X-W;
node[i].y=X+W;
}
sort(node,node+n,cmp);
int answer=1;
long long max_y=node[0].y;
for(int i=1;i<n;i++)
{
if(node[i].x>=max_y)
{
answer++;
max_y=node[i].y;
}
}
printf("%d\n",answer);
return 0;
}
#include<stdio.h>
#include<algorithm>
using namespace std;
#define maxn 200005
struct Node
{
long long x,y;
}node[maxn];
bool cmp(const Node &a,const Node &b)
{
if(a.x!=b.x)
return a.x<b.x;
else
return a.y<b.y;
}
int main()
{
int n;
long long X,W;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lld%lld",&X,&W);
node[i].x=X-W;
node[i].y=X+W;
}
sort(node,node+n,cmp);
int answer=1;
long long min_x=node[n-1].x;
for(int i=n-2;i>=0;i--)
{
if(node[i].y<=min_x)
{
answer++;
min_x=node[i].x;
}
}
printf("%d\n",answer);
return 0;
}