B. Clique Problem(贪心)
题目链接:
2 seconds
256 megabytes
standard input
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.
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.
Print a single number — the number of vertexes in the maximum clique of the given graph.
4
2 3
3 1
6 1
0 2
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(贪心)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- [CF527D] Clique Problem - 贪心
数轴上有n 个点,第i 个点的坐标为xi,权值为wi.两个点i,j之间存在一条边当且仅当 abs(xi-xj)>=wi+wj. 你需要求出这张图的最大团的点数. Solution 把每个点看作以 ...
- CodeForces - 527D Clique Problem (图,贪心)
Description The clique problem is one of the most well-known NP-complete problems. Under some simpli ...
- [codeforces 528]B. Clique Problem
[codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...
- Codeforces Round #296 (Div. 1) B - Clique Problem
B - Clique Problem 题目大意:给你坐标轴上n个点,每个点的权值为wi,两个点之间有边当且仅当 |xi - xj| >= wi + wj, 问你两两之间都有边的最大点集的大小. ...
- 回溯法——最大团问题(Maximum Clique Problem, MCP)
概述: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题.最大团问题又称为最大独立集问题(Maximum Independent ...
- codeforces 442B B. Andrey and Problem(贪心)
题目链接: B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input ...
随机推荐
- HTML 块级元素与行内元素
1.块元素一般都从新行开始,它可以容纳内联元素和其他块元素,常见块元素是段落标签'P".“form"这个块元素比较特殊,它只能用来容纳其他块元素. 2.如果没有css的作用,块元素 ...
- LeetCode:比较含退格字符串【844】
LeetCode:比较含退格字符串[844] 题目描述 给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = ...
- cmd 命令 记忆
1,“开始”—>“运行”,输入cmd,回车.<或 win+R> 2,出现“命令提示符”的窗口,一般情况下是 C:\Documents and Settings\Administrat ...
- @@fetch_status
@@fetch_status是MicroSoft SQL SERVER的一个全局变量 其值有以下三种,分别表示三种不同含义:[返回类型integer] 0 FETCH 语句成功 -1 FETCH 语句 ...
- React Native之持久化存储(AsyncStorage、react-native-storage)的使用
AsyncStorage是一个简单的.异步的.持久化的Key-Value存储系统,它对于App来说是全局性的.这是官网上对它的介绍.可以知道,这个asyncstorage也是以键值对的形式进行存储数据 ...
- 使用C语言扩展Python提供性能
python底层是用c写的,c本身是一个非常底层的语言,所以它做某些事情的效率肯定会比上层语言高一些. 比如有些自动化测试用的python库,会对系统的UI进行一些捕获,点击之类的操作,这必然要用到c ...
- Java中List集合的常用方法
List接口是继承Collection接口,所以Collection集合中有的方法,List集合也继承过来. 这篇文章就不讲继承Collection接口的那些方法了 https://www.cnblo ...
- 负载均衡之HTTP重定向
转载请说明出处:http://blog.csdn.net/cywosp/article/details/38014581 由于目前现有网络的各个核心部分随着业务量的提高,访问量和数据流量的快速增长,其 ...
- Shell 运算符 if
Shell 支持多种运算符,包括: 算术运算符 原生bash不支持简单的数学运算,可以使用 expr,let 关系运算符 布尔运算符 字符串运算符 文件运算符 算术运算符 包括加减乘除,取余(%).赋 ...
- python模块及模块安装
其实python的模块及模块安装和其他编程语言,如:nodeJs.reactJs的相同,只不过他们使用包管理工具不相同而已,python用pip,而node用npm python 模块 python语 ...