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 ...
随机推荐
- 如何优雅地使用 Stack Overflow
链接:https://www.zhihu.com/question/20824615/answer/69560657 来源:转载 一,提问前一定要搜索,先在 Google 搜索,然后在 StackOv ...
- iOS 自定义滑动切换TabbarItem 觉得设计丑也要做出来的UI效果。。。
UI丑却要继续做的感言: 对UI不满意的时候,就会觉得丑爆了,时间长了,却丑习惯了. 论前一阵子Tabbar 多丑,丑得最后不要tabbar了...但是自定义tabbar 和遇到的问题解决的过程可以记 ...
- Hadoop创始人Doug Cutting寄语2017:五种让开源项目成功的方法
原文链接:http://www.infoq.com/cn/news/2017/01/Hadoop-2017-5-open-source?utm_source=tuicool&utm_mediu ...
- OpenGL中的Shader
http://blog.csdn.net/huangcanjun187/article/details/52474365 学习总结自:http://learnopengl.com/#!Getting- ...
- 基于SSM的单点登陆05
springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- app自动化测试-appium
一.环境准备(windows) 1.安装Microsoft .NET Framework 4.5 双击运行如下文件:net4.5.1.exe 2.安装node-v6.11.4-x64.msi 双击运行 ...
- 大牛是怎么思考设计MySQL优化方案的?
在进行MySQL的优化之前,必须要了解的就是MySQL的查询过程,很多查询优化工作实际上就是遵循一些原则,让MySQL的优化器能够按照预想的合理方式运行而已. 图-MySQL查询过程 一.优化的哲学 ...
- hibernate 实体对象的三种状态以及转换关系。
最新的Hibernate文档中为Hibernate对象定义了四种状态(原来是三种状态,面试的时候基本上问的也是三种状态),分别是:瞬时态(new, or transient).持久态(managed, ...
- spark学习10(win下利用Intellij IDEA搭建spark开发环境)
第一步:启动IntelliJ IDEA,选择Create New Project,然后选择Scala,点击下一步,输入项目名称wujiadong.spark继续下一步 第二步:导入spark-asse ...
- 2.scala控制结构、函数、异常处理
2.scala控制结构.函数.异常处理---小书匠,在线编辑器,MARKDOWN,Evernote,文件版本 a:focus { outline: thin dotted #333; outline: ...