传送门

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
 #include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <set> #define ll long long
int const N = ;
int const M = ;
int const inf = ;
ll const mod = ; using namespace std; int n;
int xnow,wnow;
int ans; typedef struct
{
int x;
int w;
}PP; PP p[N]; bool cmp(PP a,PP b)
{
return a.x<b.x;
} void ini()
{
ans=;
int i;
for(i=;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].w);
}
sort(p+,p++n,cmp);
} void solve()
{
int i;
ans=;
xnow=p[].x;
wnow=p[].w;
for(i=;i<=n;i++){
if(p[i].x-xnow>=p[i].w+wnow){
xnow=p[i].x;
wnow=p[i].w;
ans++;
}
else{
if(p[i].x+p[i].w<xnow+wnow){
xnow=p[i].x;
wnow=p[i].w;
}
}
}
} void out()
{
printf("%d\n",ans);
} int main()
{
// freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(cnt=1;cnt<=T;cnt++)
while(scanf("%d",&n)!=EOF)
{
ini();
solve();
out();
}
}

Codeforces Round #296 (Div. 2) D. Clique Problem [ 贪心 ]的更多相关文章

  1. 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 ...

  2. Codeforces Round #296 (Div. 1) B - Clique Problem

    B - Clique Problem 题目大意:给你坐标轴上n个点,每个点的权值为wi,两个点之间有边当且仅当 |xi - xj| >= wi + wj, 问你两两之间都有边的最大点集的大小. ...

  3. 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 ...

  4. Codeforces Round #296 (Div. 1) C. Data Center Drama 欧拉回路

    Codeforces Round #296 (Div. 1)C. Data Center Drama Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xx ...

  5. Codeforces Round #367 (Div. 2) C. Hard problem

    题目链接:Codeforces Round #367 (Div. 2) C. Hard problem 题意: 给你一些字符串,字符串可以倒置,如果要倒置,就会消耗vi的能量,问你花最少的能量将这些字 ...

  6. Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题

    Codeforces Round #603 (Div. 2) A. Sweet Problem A. Sweet Problem time limit per test 1 second memory ...

  7. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  8. Codeforces Round #296 (Div. 1) E. Triangles 3000

    http://codeforces.com/contest/528/problem/E 先来吐槽一下,一直没机会进div 1, 马力不如当年, 这场题目都不是非常难,div 2 四道题都是水题! 题目 ...

  9. Codeforces Round #361 (Div. 2) C.NP-Hard Problem

    题目连接:http://codeforces.com/contest/688/problem/C 题意:给你一些边,问你能否构成一个二分图 题解:二分图:二分图又称作二部图,是图论中的一种特殊模型. ...

随机推荐

  1. [BZOJ1968][AHOI2005]COMMON约数研究 数学

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1968 直接计算每个因子的贡献就可以了. $Ans=\sum_{i=1}^n[\frac{n ...

  2. IOS OS X 中集中消息的传递机制

    1 KVO (key-value Observing) 是提供对象属性被改变是的通知机制.KVO的实现实在Foundation中,很多基于 Foundation 的框架都依赖与它.如果只对某一个对象的 ...

  3. 嵌入式ARM开发板学习方法步骤

    嵌入式开发就是指在嵌入式操作系统下进行开发,一般常用的系统有linux,android. 平台:Cortex-A9开发板 嵌入式技术学习如何入手,从何学起呢, 以下内容简单介绍嵌入式开发的学习步骤及如 ...

  4. 因JQUERY版本而产生的问题,需要加上迁移文件

    IMG_01_history控制台报错 IMG_02_history代码报错

  5. qs库 是将url参数和json互转 | query strings 缩写 | import qs from 'qs'

    import qs from 'qs'   1.npm地址 https://www.npmjs.com/package/qs 2.概述 将url中的参数转为对象: 将对象转为url参数形式 3.示例 ...

  6. C++虚析构函数的使用

    如果,你设计的程序里,释放对象实例的时候,有“使用某个基类的指针,来释放它指向的派生类的实例”这种用法出现的话,那么,这个基类的destructor就应该设计成virtual的. 如果,基类不是vir ...

  7. win10x64下的redis安装与使用

    先引用百度百科的一段话吧,具体可以到百科查看吧. Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年 ...

  8. 线程的start和run方法的区别

    回到这个问题,可以用源码的角度去回答,这样会让面试官对有更好的印象 ------>如果直接调用run方法的话,所执行的线程是main线程.调用start方法的话,会新建一个子线程,去执行run方 ...

  9. sublime text 3 安装Nodejs插件

    如题 1)集成Nodejs插件到sublime,地址:https://github.com/tanepiper/SublimeText-Nodejs2)解压zip文件, 并重命名文件夹“Nodejs” ...

  10. mysql查询速度慢的原因[整理版]

    在以前的博客中陆续记录了有关查询效率方面的文章.今天在整理一下,写上自己的一些心得记录如下:常见查询慢的原因常见的话会有如下几种:1.没有索引或没有用到索引.PS:索引用来快速地寻找那些具有特定值的记 ...