Codeforces Round #296 (Div. 1)B---Clique Problem

68 篇文章 0 订阅
48 篇文章 0 订阅

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.

dp[i][0] 表示 不选第i个点, dp[i][1] 表示选入第i个点
dp[i][0] = max(dp[i][1], dp[i][0]);
对点按坐标排序
对于当前第i个点, 它可以建边的点的坐标一定小于等于xi - wi
如果第j个点可以和第i个点建边,有xj + wj <= xi - wi
数据很大,因此考虑线段树优化
每次求好dp[i][1],把他放到编号为xi+wi 的叶子节点上
这些点得离散化

/*************************************************************************
    > File Name: CF-296-B.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年03月18日 星期三 19时59分47秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 200100;
int dp[N][2];
int xis[3 * N];
int cnt;

struct node
{
    int l, r;
    int val;
}tree[N << 4];

struct point
{
    int wi;
    int xi;
}mp[N];

int cmp (point a, point b)
{
    return a.xi < b.xi;
}

int search(int val)
{
    int mid;
    int l = 1;
    int r = cnt;
    while (l <= r)
    {
        mid = (l + r) >> 1;
        if (xis[mid] > val)
        {
            r = mid - 1;
        }
        else if (xis[mid] < val)
        {
            l = mid + 1;
        }
        else
        {
            break;
        }
    }
    return mid;
}

void build(int p, int l, int r)
{
    tree[p].l = l;
    tree[p].r = r;
    if (l == r)
    {
        tree[p].val = 0;
        return;
    }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    tree[p].val = max(tree[p << 1].val, tree[p << 1 | 1].val);
}

void update(int p, int pos, int val)
{
    if (tree[p].l == tree[p].r)
    {
        tree[p].val = max(tree[p].val, val);
        return;
    }
    int mid = (tree[p].l + tree[p].r) >> 1;
    if (pos <= mid)
    {
        update(p << 1, pos, val);
    }
    else
    {
        update(p << 1 | 1, pos, val);
    }
    tree[p].val = max(tree[p << 1].val, tree[p << 1 | 1].val);
}

int query (int p, int l, int r)
{
    if (l == tree[p].l && r == tree[p].r)
    {
        return tree[p].val;
    }
    int mid = (tree[p].l + tree[p].r) >> 1;
    if (r <= mid)
    {
        return query(p << 1, l, r);
    }
    else if (l > mid)
    {
        return query (p << 1 | 1, l, r);
    }
    else
    {
        int a = query (p << 1, l, mid);
        int b = query (p << 1 | 1, mid + 1, r);
        return max(a, b);
    }
}

int main ()
{
    int n;
    while (~scanf("%d", &n))
    {
        cnt = 0;
        memset (dp, 0, sizeof(dp));
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d%d", &mp[i].xi, &mp[i].wi);
            xis[++cnt] = mp[i].xi - mp[i].wi;
            xis[++cnt] = mp[i].xi + mp[i].wi;
        }
        sort (mp + 1, mp + 1 + n, cmp);
        sort (xis + 1, xis + 1 + cnt);
        cnt = unique (xis + 1, xis + cnt + 1) - xis - 1;
        build(1, 1, cnt);
        for (int i = 1; i <= n; ++i)
        {
            dp[i][0] = max(dp[i - 1][1], dp[i - 1][0]);
            int r = search(mp[i].xi - mp[i].wi);
            dp[i][1] = query(1, 1, r) + 1;
            r = search(mp[i].xi + mp[i].wi);
            update(1, r, dp[i][1]);
        }
        printf("%d\n", max(dp[n][0], dp[n][1]));
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值