题目链接:

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;
}

  

 

B. Clique Problem(贪心)的更多相关文章

  1. CF #296 (Div. 1) B. Clique Problem 贪心(构造)

    B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. Codeforces Round #296 (Div. 1) B. Clique Problem 贪心

    B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. Codeforces Round #296 (Div. 2) D. Clique Problem [ 贪心 ]

    传送门 D. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  4. [CF527D] Clique Problem - 贪心

    数轴上有n 个点,第i 个点的坐标为xi,权值为wi.两个点i,j之间存在一条边当且仅当 abs(xi-xj)>=wi+wj. 你需要求出这张图的最大团的点数. Solution 把每个点看作以 ...

  5. CodeForces - 527D Clique Problem (图,贪心)

    Description The clique problem is one of the most well-known NP-complete problems. Under some simpli ...

  6. [codeforces 528]B. Clique Problem

    [codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...

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

    B - Clique Problem 题目大意:给你坐标轴上n个点,每个点的权值为wi,两个点之间有边当且仅当 |xi - xj| >= wi + wj, 问你两两之间都有边的最大点集的大小. ...

  8. 回溯法——最大团问题(Maximum Clique Problem, MCP)

    概述: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题.最大团问题又称为最大独立集问题(Maximum Independent ...

  9. codeforces 442B B. Andrey and Problem(贪心)

    题目链接: B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. Rest_framework-1

    目录 一.认证 二.权限 三.限制访问频率 四.总结 一.认证(补充) 认证请求头 #!/usr/bin/env python # -*- coding:utf-8 -*- from rest_fra ...

  2. ZooKeeper的使用:安装、常用的命令

    公司项目需要使用dubbo,因此,自己做个小Demo就很有必要了,也有助于自己理解和使用,前期准备工作当然就必不可少了,因为dubbo是发布到zookeeper的服务,故先把zookeeper的环境先 ...

  3. Google ProtocolBuffer

    https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html 1. Protocol Buffers 简介 Protocol Buff ...

  4. svn / git SourceTree

    开发使用SourceTree 忽略文件这块老弄错,这次专门博客一下,使用CocoaPods 开发项目, 忽略步骤如下:  忽略文件内容如下 *.xcworkspace xcuserdata *.loc ...

  5. js 性能优化 篇一

    JS性能优化 摘自:http://www.china125.com/design/js/3631.htm  首先,由于JS是一种解释型语言,执行速度要比编译型语言慢得多.(注:,Chrome是第一款内 ...

  6. h5打开App的方法。

    在浏览器中: 法1: location.href = `${scheme}`;//location跳转App是几乎所以情况都支持的. 法2: var ifr = document.createElem ...

  7. VRChat简易教程2-创建一个最基本的世界(world)

    一.准备工作 1 先确保你安装了unity并导入了sdk 教程:https://www.cnblogs.com/cation/p/10311702.html 2 按之前的教程新建一个project并导 ...

  8. 20145201《Java程序设计》第九周学习总结

    20145201 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无须接触底层 ...

  9. java jvm内存管理/gc策略/参数设置

    1. JVM内存管理:深入垃圾收集器与内存分配策略 http://www.iteye.com/topic/802638 Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的高墙,墙外面的人想 ...

  10. [BZOJ2730]矿场搭建

    Description 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无论哪一 ...