【题解】codeforces528B[AHSOFNU codeforces训练赛1 by hzwer]D.Clique Problem 排序

32 篇文章 0 订阅
25 篇文章 0 订阅

Description

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.
在这里插入图片描述


由题意满足 ∣ x i − x j ∣ ≥ w i + w j |x_i-x_j|\geq w_i+w_j xixjwi+wj的点对 ( i , j ) (i,j) (i,j) 可以连边。对公式化简得 x i − w i ≥ x j + w j x_i-w_i\geq x_j+w_j xiwixj+wj x i + w i ≤ x j − w j x_i+w_i\leq x_j-w_j xi+wixjwj。所以把所有点按照 x i + w i x_i+w_i xi+wi 升序排序,和前面的点比较。

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=2e5+10;
int n;
ll x,w;
pair<ll,ll>p[N];
int main()
{
	//freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%lld%lld",&x,&w),p[i]=make_pair(x+w,x-w);
	sort(p+1,p+n+1);
	ll pre=-0x7fffffff,cnt=0;
	for(int i=1;i<=n;i++)
	    if(p[i].second>=pre)cnt++,pre=p[i].first;
	printf("%lld\n",cnt);
	return 0;
}

总结

主考对公式的化简。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值