首页 > 其他 > 详细

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

时间:2015-10-12 17:20:07      阅读:246      评论:0      收藏:0      [点我收藏+]

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, doesnt 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. Lets 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 ≤ 200000) — 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
4
2 3
3 1
6 1
0 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<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
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 long
20 #define eps 1e-10
21 #define MOD 1000000007
22 #define N 200006
23 #define inf 1e12
24 int n;
25 struct Node{
26    int x,w;
27 }node[N];
28 struct Node1{
29    int num1,num2;
30 };
31 vector<Node1 > G;
32 
33 bool cmp(Node1 a,Node1 b){
34    if(a.num1!=b.num1) return a.num1<b.num1;
35    return a.num2<b.num2;
36 }
37 
38 int main()
39 {
40    while(scanf("%d",&n)==1){
41        for(int i=0;i<n;i++){
42            scanf("%d%d",&node[i].x,&node[i].w);
43        }
44        G.clear();
45        for(int i=0;i<n;i++){
46            int num1=node[i].x+node[i].w;
47            int num2=node[i].x-node[i].w;
48            Node1 tmp;
49            tmp.num1=num1;
50            tmp.num2=num2;
51            G.push_back(tmp);
52        }
53        sort(G.begin(),G.end(),cmp);
54        int ans=1;
55        Node1 tmp=G[0];
56        for(int i=1;i<G.size();i++){
57            if(tmp.num1<=G[i].num2){
58               ans++;
59               tmp=G[i];
60            }
61        }
62 
63        printf("%d\n",ans);
64    }
65     return 0;
66 }
View Code

 

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

原文:http://www.cnblogs.com/UniqueColor/p/4872054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!