贪心(最大团)||dp+线段树(Codeforces Round #296 (Div. 2)D. Clique Problem)

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

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+wi的点和小于xi-wi的点有边,如果所有点的xi+wi,xi-wi都加到队列中,那么我们只需要找一个最长上升序列就是答案

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=200010;
const int INF=2000000000;
int x[maxn],w[maxn];
int N;
struct node
{
    int l,r;
    bool operator<(const node &a)const
    {
        if(l==a.l)return r<a.r;
        return l<a.l;
    }
}a[2*maxn];
int main()
{
    while(scanf("%d",&N)!=EOF)
    {
        int cnt=0;
        for(int i=1;i<=N;i++)
        {
            scanf("%d%d",&x[i],&w[i]);
            a[i].l=x[i]-w[i];
            a[i].r=x[i]+w[i];
        }
        sort(a+1,a+N+1);
        int ans=0;
        node tmp;
        tmp.l=tmp.r=-INF;
        for(int i=1;i<=N;i++)
        {
            if(a[i].l>=tmp.r)
            {
                ans++;
                tmp=a[i];
            }
            else if(tmp.r>a[i].r)
                tmp=a[i];
        }
        printf("%d\n",ans);
    }
    return 0;
}

线段树+dp写法跟上面的思想是一样的,dp[i][0]表示不包括这个点的最大团,dp[i][1]表示包括这个点的最大团

下面的代码是别人交的,学习一下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int MOD=1e9+7;
const int INF = 0x3f3f3f3f;
const int maxn=400000+100;
int dp[maxn/2][2],n;
int sum[maxn<<2];
LL b[maxn];
struct node{
    LL x,w;
}e[maxn];
void pushup(int rs)
{
    sum[rs]=max(sum[rs<<1],sum[rs<<1|1]);
}
void build(int rs,int l,int r)
{
    sum[rs]=0;
    if(l==r)  return ;
    int mid=(l+r)>>1;
    build(rs<<1,l,mid);
    build(rs<<1|1,mid+1,r);
    pushup(rs);
}
void update(int x,int c,int l,int r,int rs)
{
    if(l==r)
    {
        sum[rs]=c;
        return ;
    }
    int mid=(l+r)>>1;
    if(x<=mid) update(x,c,l,mid,rs<<1);
    else  update(x,c,mid+1,r,rs<<1|1);
    pushup(rs);
}
int query(int x,int y,int l,int r,int rs)
{
    if(l>=x&&r<=y)
        return sum[rs];
    int mid=(l+r)>>1;
    int ans=0;
    if(x<=mid)  ans=max(ans,query(x,y,l,mid,rs<<1));
    if(y>mid)  ans=max(ans,query(x,y,mid+1,r,rs<<1|1));
    return  ans;
}
int cmp(node l1,node l2)
{
    if(l1.x==l2.x) return l1.w<l2.w;
    return l1.x<l2.x;
}
int main()
{
    while(~scanf("%d",&n))
    {
        int cnt=0;
        REPF(i,1,n)
        {
            scanf("%lld%lld",&e[i].x,&e[i].w);
            b[cnt++]=e[i].x-e[i].w;
            b[cnt++]=e[i].x+e[i].w;
        }
        sort(e+1,e+1+n,cmp);
        sort(b,b+cnt);
        cnt=unique(b,b+cnt)-b;
        build(1,1,cnt);
        CLEAR(dp,0);
        for(int i=1;i<=n;i++)
        {
            int x1=lower_bound(b,b+cnt,e[i].x-e[i].w)-b+1;
            int x2=lower_bound(b,b+cnt,e[i].x+e[i].w)-b+1;
            dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
            dp[i][1]=query(1,x1,1,cnt,1)+1;
            update(x2,dp[i][1],1,cnt,1);
        }
        printf("%d\n",max(dp[n][0],dp[n][1]));
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值