题目链接:

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. python全栈开发从入门到放弃之网络基础

    一.操作系统基础 操作系统:(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才 ...

  2. 微信小程序组件form

    表单组件form:官方文档 Demo Code: Page({ formSubmit: function(e) { console.log('form发生了submit事件,携带数据为:', e.de ...

  3. [笔记] Ubuntu下编译ffmpeg+openh264+x264

    [下载代码]   - ffmpeg: git clone git://source.ffmpeg.org/ffmpeg.git - openh264: git clone https://github ...

  4. maven打包生成war

  5. Educational Codeforces Round 11C. Hard Process two pointer

    地址:http://codeforces.com/contest/660/problem/C 题目: You are given an array a with n elements. Each el ...

  6. 使用Vue.js初次真正项目开发-2018/07/14

    一.组件化 使用Vue.js进行开发,按照MVVM模式,围绕数据为核心,进行开发. 开发过程根据业务和功能组件化,组件化一方面让我们开发思路更加清晰,另一方面对于数据的处理和控制变得更加简单,毕竟一个 ...

  7. C# winform 屏蔽鼠标右键 spreadsheet Gear 屏蔽鼠标右键菜单

    今天用到spreadsheetGear 插件,然后右键有插件自己的菜单.都是英文的,而且还能打开新的窗体.嵌到程序里面,不太合适,所以着手屏蔽. 刚开始用的Mouse_up,虽然能捕获事件,但是没有K ...

  8. CSS3鼠标悬停边框线条动画按钮

    在线演示 本地下载

  9. SpringBoot和Mycat动态数据源项目整合

    SpringBoot项目整合动态数据源(读写分离) 1.配置多个数据源,根据业务需求访问不同的数据,指定对应的策略:增加,删除,修改操作访问对应数据,查询访问对应数据,不同数据库做好的数据一致性的处理 ...

  10. get the request body of all quests before handle it

    https://stackoverflow.com/questions/23660340/need-to-log-asp-net-webapi-2-request-and-response-body- ...