B. 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 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.

Examples
input
4
2 3
3 1
6 1
0 2
output
3

题意:满足上面的式子的点对连一条边,问连完边后最大独立团的点数是多少;
思路:假设xi>=xj,那么xi-wi>=xj+wj,那么按x排序后,对于每一个点就可以与<=xi-wi区间的点相连(这些点区间假设为[l,r]),
那么[l,r]区间的最大团数目加1就可以更新当前点的值了;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int n,dp[maxn];
std::vector<int> ve;
struct node
{
    int x,w;
}po[maxn];
int cmp(node a,node b){return a.x<b.x;}
struct Tree
{
    int l,r,mx;
}tr[4*maxn];
void build(int o,int L,int R)
{
    tr[o].l=L;tr[o].r=R;tr[o].mx=1;
    if(L>=R)return ;
    int mid=(tr[o].l+tr[o].r)>>1;
    build(2*o,L,mid);build(2*o+1,mid+1,R);
}
int query(int o,int L,int R)
{
    if(L<=tr[o].l&&R>=tr[o].r)return tr[o].mx;
    int ans=0;
    int mid=(tr[o].l+tr[o].r)>>1;
    if(L<=mid)ans=max(ans,query(2*o,L,R));
    if(R>mid)ans=max(ans,query(2*o+1,L,R));
    return ans;
}
void update(int o,int pos,int num)
{
    if(tr[o].l==tr[o].r&&tr[o].l==pos){tr[o].mx=num;return ;}
    int mid=(tr[o].l+tr[o].r)>>1;
    if(pos<=mid)update(2*o,pos,num);
    else update(2*o+1,pos,num);
    tr[o].mx=max(tr[2*o].mx,tr[2*o+1].mx);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d%d",&po[i].x,&po[i].w),ve.push_back(po[i].x+po[i].w),dp[i]=1;
    sort(po+1,po+n+1,cmp);
    sort(ve.begin(),ve.end());
    build(1,1,n);
    for(int i=1;i<=n;i++)
    {
        int tep=po[i].x-po[i].w;
        int pos=upper_bound(ve.begin(),ve.end(),tep)-ve.begin();
        int p=lower_bound(ve.begin(),ve.end(),po[i].x+po[i].w)-ve.begin()+1;
        if(pos>0)dp[p]=max(dp[p],query(1,1,pos)+1);
        update(1,p,dp[p]);
    }
    printf("%d\n",query(1,1,n));
    return 0;
}

  

 
posted @ 2017-05-14 21:56  LittlePointer  阅读(215)  评论(0编辑  收藏  举报