CodeForces - 527D Clique Problem (图,贪心)
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 ( ≤ n ≤ ) — the number of points. Each of the next n lines contains two numbers xi, wi ( ≤ xi ≤ , ≤ wi ≤ ) — 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
假设点Xi>Xj,那么绝对值符号可以去掉,即Xi-Xj≥Wi+Wj。移项可以得到Xi-Wi≥Xj+Wj。这样的话,其实就确定了一个有向图的关系,题目转化为找结点数最多的有向图。运用贪心的思想,肯定希望第一个结点的坐标尽量小,以便于容纳更多的结点。因此事先计算出P(X+W,X-W)后放入vector,排序后从第一个点开始尝试,只要满足这样的关系式就努力往后拓展。这样得到的有向图结点数一定是最多的。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 200006
#define inf 1e12
int n;
struct Node{
int x,w;
}node[N];
struct Node1{
int num1,num2;
};
vector<Node1 > G; bool cmp(Node1 a,Node1 b){
if(a.num1!=b.num1) return a.num1<b.num1;
return a.num2<b.num2;
} int main()
{
while(scanf("%d",&n)==){
for(int i=;i<n;i++){
scanf("%d%d",&node[i].x,&node[i].w);
}
G.clear();
for(int i=;i<n;i++){
int num1=node[i].x+node[i].w;
int num2=node[i].x-node[i].w;
Node1 tmp;
tmp.num1=num1;
tmp.num2=num2;
G.push_back(tmp);
}
sort(G.begin(),G.end(),cmp);
int ans=;
Node1 tmp=G[];
for(int i=;i<G.size();i++){
if(tmp.num1<=G[i].num2){
ans++;
tmp=G[i];
}
} printf("%d\n",ans);
}
return ;
}
CodeForces - 527D Clique Problem (图,贪心)的更多相关文章
- Codeforces 527D Clique Problem
http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...
- 527D.Clique Problem
题解: 水题 两种做法: 1.我的 我们假设$xi>xj$ 那么拆开绝对值 $$xi-w[i]>x[j]+w[j]$$ 由于$w[i]>0$,所以$x[i]+w[i]>x[j] ...
- 527D Clique Problem 判断一维线段没有两辆相交的最大线段数量
这题说的是给了n个位置 在x轴上 每个位置有一个权值为wi,然后将|xi - xj|>=wi+wj ,满足这个条件的点建一条边,计算着整张图中有多少多少个点构成的子图,使得这个子图的节点数尽量的 ...
- 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 ...
- [codeforces 528]B. Clique Problem
[codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...
- 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 题目大意:给你坐标轴上n个点,每个点的权值为wi,两个点之间有边当且仅当 |xi - xj| >= wi + wj, 问你两两之间都有边的最大点集的大小. ...
- B. Clique Problem(贪心)
题目链接: B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- HTML5 canvas translate() 方法
HTML5 canvas translate() 方法 translate() 方法重新映射画布上的 (0,0) 位置.
- [每日一题] 11gOCP 1z0-052 :2013-09-25 Lock ――for update.................................C23
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12021587 正确答案:ABE 这道题需要我们了解锁的知识点. TM锁的模式: 0-Non ...
- poj 2115 C Looooops(推公式+扩展欧几里得模板)
Description A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; ...
- JAVA常见异常集锦(持续更新)
No1:Nested in org.springframework.beans.factory.parsing.BeanDefinitionParsingException 2013-07-02 10 ...
- C++中的初始化列表中可以对那些变量或对象进行初始化
构造函数与其函数体之间可以添加初始化列表,能对某些对象进行初始化.格式为 类名() : 变量1(参数1),变量2(参数2) { } 1. 父类的对象的构造必须在初始化列表中,如: 子类名(): ...
- Spting使用memcached
applicationContext.xml配置文件: <?xml version="1.0" encoding="UTF-8"?> <bea ...
- SSH2配置事务的两种方式
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- [有向图的强连通分量][Tarjan算法]
https://www.byvoid.com/blog/scc-tarjan 主要思想 Tarjan算法是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树.搜索时,把当前搜索树中未处理的 ...
- Hadoop: Start-all.sh 后发现JPS后Namenode没有启动
重新格式化Namenode:hadoop namenode -format 然后启动hadoop:start-all.sh 执行下JPS命令就可以看到NameNode了
- jdbc——CallableStatement,cst调用存储过程
package CST对象调用存储过程; import java.sql.CallableStatement; import java.sql.Types; import org.junit.Test ...