Codeforces Round #296 (Div. 2) D. Clique Problem [ 贪心 ]

传送门

D. 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 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 test(s)
Input
4
2 3
3 1
6 1
0 2
Output
3
Note

If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!

The picture for the sample test.

题意:

选出一个最大的集合,集合里面的点两两之间满足:|xi - xj| ≥ wi + wj.

题解:

贪心,先按x排序。设xj<xi<xk

若xi-xj>=wi+wj;

xk-xi>=wk+wi;

则xk-xj>=wk+wj+2*wi>=wk+wj。

故相邻的点之间满足条件,则所有点均满足条件。

继续,xi-xj>=wi+wj; 等价于 xi-wi>=xj+wj;

即前一个点的x+w的和要尽可能小。

1.若当前点与前一个点满足条件,则ans++,xnow=x[i],wnow=w[i];

2.若与前一个点不满足条件,则看 x+w的和的关系,若x+w<xnow+wnow,由于x是递增关系,x-w必然更大,则与前一个点满足条件的集合,肯定也与当前点满足。

故舍弃前一个点,取当前点xnow=x[i],wnow=w[i];  反之,舍弃当前点。

10349119 2015-03-19 16:14:45 njczy2010 D - Clique Problem GNU C++ Accepted 93 ms 1384 KB
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <stack>
 4 #include <vector>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <map>
 8 #include <string>
 9 #include <set>
10 
11 #define ll long long
12 int const N = 200005;
13 int const M = 205;
14 int const inf = 1000000000;
15 ll const mod = 1000000007;
16 
17 using namespace std;
18 
19 int n;
20 int xnow,wnow;
21 int ans;
22 
23 typedef struct
24 {
25     int x;
26     int w;
27 }PP;
28 
29 PP p[N];
30 
31 bool cmp(PP a,PP b)
32 {
33     return a.x<b.x;
34 }
35 
36 void ini()
37 {
38     ans=0;
39     int i;
40     for(i=1;i<=n;i++){
41         scanf("%d%d",&p[i].x,&p[i].w);
42     }
43     sort(p+1,p+1+n,cmp);
44 }
45 
46 void solve()
47 {
48     int i;
49     ans=1;
50     xnow=p[1].x;
51     wnow=p[1].w;
52     for(i=2;i<=n;i++){
53         if(p[i].x-xnow>=p[i].w+wnow){
54             xnow=p[i].x;
55             wnow=p[i].w;
56             ans++;
57         }
58         else{
59             if(p[i].x+p[i].w<xnow+wnow){
60                 xnow=p[i].x;
61                 wnow=p[i].w;
62             }
63         }
64     }
65 }
66 
67 void out()
68 {
69     printf("%d
",ans);
70 }
71 
72 int main()
73 {
74   //  freopen("data.in","r",stdin);
75     //scanf("%d",&T);
76     //for(cnt=1;cnt<=T;cnt++)
77     while(scanf("%d",&n)!=EOF)
78     {
79         ini();
80         solve();
81         out();
82     }
83 }
原文地址:https://www.cnblogs.com/njczy2010/p/4351832.html