Codefroces 527D Clique Problem【思维+贪心】

B. Clique Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

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.



题目大意:

一共有N个点,每个点的位子以及其权值wi已知,如果两个点之间的距离大于两个点的权值和,那么对应两个点就可以连一条边,求一个最大团。


思路:


1、不知道最大团的小伙伴们直接去百度就好。


2、接下来我们分析问题:

首先分析不等式:

①|xi - xj| ≥ wi + wj-------------------->xi - xj ≥ wi + wj.------------------------>xi -wi ≥xj+ wj.

那么我们考虑设定xi-wi==X,xi+wi==Y,那么很显然,如果存在点对(i,j),其有Xi>=Yi,那么这两个点之间一定可以建立一条边。

然后我们分析一个性质:

②如果存在两个点,i,j使得有存在:Dis(i,j)>=wi+wj并且存在另外两个点,j,k,使得有存在:Dis(j,k)>=wj+wk,那么一定有:

Dis(i,j)+Dis(j,k)>=wi+2wj+wk--------------------->Dis(i,k)>=wi+wk,也就是说其存在递推性质。

③那么我们可以贪心,将所有点按照Y从小到大排序(如果Y相等,按照X从小到大排序),那么我们接下来O(n)维护此时最大的Y即可,记做maxn。如果存在一个点,其Xi>=maxn,那么更新maxn=Yi,同时ans++。


Ac代码:


#include
#include
#include
using namespace std;
struct node
{
    int x,y;
}a[200060];
int cmp(node a,node b)
{
    if(a.y!=b.y)return a.y=maxn)
            {
                maxn=a[i].y;
                ans++;
            }
        }
        printf("%d\n",ans);
    }
}










你可能感兴趣的:(贪心,思维)