博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces - 527D Clique Problem (图,贪心)
阅读量:5807 次
发布时间:2019-06-18

本文共 2309 字,大约阅读时间需要 7 分钟。

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.

 

 

 

Sample Input

Input
42 33 16 10 2

 

Output
3

 

    假设点Xi>Xj,那么绝对值符号可以去掉,即Xi-Xj≥Wi+Wj。移项可以得到Xi-Wi≥Xj+Wj。这样的话,其实就确定了一个有向图的关系,题目转化为找结点数最多的有向图。运用贪心的思想,肯定希望第一个结点的坐标尽量小,以便于容纳更多的结点。因此事先计算出P(X+W,X-W)后放入vector,排序后从第一个点开始尝试,只要满足这样的关系式就努力往后拓展。这样得到的有向图结点数一定是最多的。

 

1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 using namespace std;16 #define PI acos(-1.0)17 #define max(a,b) (a) > (b) ? (a) : (b)18 #define min(a,b) (a) < (b) ? (a) : (b)19 #define ll long long20 #define eps 1e-1021 #define MOD 100000000722 #define N 20000623 #define inf 1e1224 int n;25 struct Node{26 int x,w;27 }node[N];28 struct Node1{29 int num1,num2;30 };31 vector
G;32 33 bool cmp(Node1 a,Node1 b){34 if(a.num1!=b.num1) return a.num1
View Code

 

转载地址:http://gekbx.baihongyu.com/

你可能感兴趣的文章
TCP segmentation offload
查看>>
java数据类型
查看>>
数据结构——串的朴素模式和KMP匹配算法
查看>>
FreeMarker-Built-ins for strings
查看>>
验证DataGridView控件的数据输入
查看>>
POJ1033
查看>>
argparse - 命令行选项与参数解析(转)
查看>>
一维数组
查看>>
Linux学习笔记之三
查看>>
关于Android studio团队协同开发连接到已有项目
查看>>
Sql获取表的信息
查看>>
Java-大数据-图汇集
查看>>
一、数论算法
查看>>
Asp.net MVC 中Controller的返回类型大全
查看>>
用一条SQL语句实现斐波那契数列
查看>>
[高中作文赏析]跋涉与成功
查看>>
swift-辞典NSDictionary定义,变化的关键,删/加入关键
查看>>
python----slots属性安全类
查看>>
《Programming WPF》翻译 第5章 1.不使用样式
查看>>
.NET垃圾回收:非托管资源,IDispose和析构函数的结合
查看>>