codforeces 527D 貪心

D. 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 graphG, 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 xiwi (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.

Sample test(s)
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.


滿足|xi-xj| >= wi+wj  -> xi-wi >= xj+wj則i,j連接一條邊。 將所有的數據xi,wi轉換成(xi-wi,xi+wi)對。第i個點和前面的點連接時,只需要考慮前xj+wj的最大值就可以了,若大於xj+wj,則前面與j相連的也可以與i相連。把(xi-wi,xi+wi)看成區間的話,求最大的團,其實就是求最多的互不覆蓋的區間個數,因此按xi+wi排序,貪心的選擇就行了。線性複雜度。


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

#define maxn 200005
#define inf 0x3f3f3f3f

typedef pair<int,int> p;

p a[maxn];
int n;

int cmp (const p &a , const p &b)
{
    return a.second < b.second;
}

int main()
{
    while(~scanf("%d", &n)){
        int x,w;
        for(int i = 0; i < n; i++){
            scanf("%d%d", &x, &w);
            a[i] = p(x-w,x+w);
        }

        sort(a, a+n,cmp);
        int pre = -inf;
        int ans = 0;
        for(int i = 0; i < n; i++){
            if(a[i].first >= pre)
                ans++, pre = a[i].second;
        }

        printf("%d\n",ans);
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章