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

针对一种特殊类型的图,探讨如何高效地找到最大团。通过分析点的位置与权重关系,利用贪心策略解决NP完全问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int x,y;
}a[200060];
int cmp(node a,node b)
{
    if(a.y!=b.y)return a.y<b.y;
    else return a.x<b.x;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
        {
            int pos;int w;
            scanf("%d%d",&pos,&w);
            a[i].x=pos-w;
            a[i].y=pos+w;
        }
        sort(a,a+n,cmp);
        int ans=1;
        int maxn=a[0].y;
        for(int i=1;i<n;i++)
        {
            if(a[i].x>=maxn)
            {
                maxn=a[i].y;
                ans++;
            }
        }
        printf("%d\n",ans);
    }
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值