CodeForces - 527D Clique Problem (图,贪心)

Description

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 ( ≤ n ≤  ) — the number of points.

Each of the next n lines contains two numbers xi, wi ( ≤ xi ≤ ,  ≤ wi ≤ ) — 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.

Sample Input

Input

Output

    假设点Xi>Xj,那么绝对值符号可以去掉,即Xi-Xj≥Wi+Wj。移项可以得到Xi-Wi≥Xj+Wj。这样的话,其实就确定了一个有向图的关系,题目转化为找结点数最多的有向图。运用贪心的思想,肯定希望第一个结点的坐标尽量小,以便于容纳更多的结点。因此事先计算出P(X+W,X-W)后放入vector,排序后从第一个点开始尝试,只要满足这样的关系式就努力往后拓展。这样得到的有向图结点数一定是最多的。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 200006
#define inf 1e12
int n;
struct Node{
int x,w;
}node[N];
struct Node1{
int num1,num2;
};
vector<Node1 > G; bool cmp(Node1 a,Node1 b){
if(a.num1!=b.num1) return a.num1<b.num1;
return a.num2<b.num2;
} int main()
{
while(scanf("%d",&n)==){
for(int i=;i<n;i++){
scanf("%d%d",&node[i].x,&node[i].w);
}
G.clear();
for(int i=;i<n;i++){
int num1=node[i].x+node[i].w;
int num2=node[i].x-node[i].w;
Node1 tmp;
tmp.num1=num1;
tmp.num2=num2;
G.push_back(tmp);
}
sort(G.begin(),G.end(),cmp);
int ans=;
Node1 tmp=G[];
for(int i=;i<G.size();i++){
if(tmp.num1<=G[i].num2){
ans++;
tmp=G[i];
}
} printf("%d\n",ans);
}
return ;
}
上一篇:【iCore3 双核心板_FPGA】实验十四:FSMC总线通信实验——独立地址模式


下一篇:孤荷凌寒自学python第三十三天python的文件操作初识