[CF527D] Clique Problem - 贪心
数轴上有n 个点,第i 个点的坐标为xi,权值为wi。两个点i,j之间存在一条边当且仅当 abs(xi-xj)>=wi+wj。 你需要求出这张图的最大团的点数。
Solution
把每个点看作以 \((x_i,0)\) 为圆心,半径为 \(r_i\) 的圆
那么如果不相交就有边相连
干脆看作线段吧,所以就是求最大不相交线段数
这就是一个很基础的贪心,以右端点为第一关键字,左端点为第二关键字 sort
然后“能取就取”即可
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 400005;
int n,x[N],w[N];
struct interval {
int l,r;
bool operator < (const interval &b) const {
return r < b.r;
}
} I[N];
signed main() {
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++) cin>>x[i]>>w[i];
for(int i=1;i<=n;i++) {
I[i].l=x[i]-w[i];
I[i].r=x[i]+w[i];
}
sort(I+1,I+n+1);
int pos=-1e9,ans=0;
for(int i=1;i<=n;i++) {
if(I[i].l>=pos) {
++ans;
pos=I[i].r;
}
}
cout<<ans;
}
[CF527D] Clique Problem - 贪心的更多相关文章
- 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 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 ...
- B. Clique Problem(贪心)
题目链接: B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces - 527D Clique Problem (图,贪心)
Description The clique problem is one of the most well-known NP-complete problems. Under some simpli ...
- [codeforces 528]B. Clique Problem
[codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...
- Codeforces Round #296 (Div. 1) B - Clique Problem
B - Clique Problem 题目大意:给你坐标轴上n个点,每个点的权值为wi,两个点之间有边当且仅当 |xi - xj| >= wi + wj, 问你两两之间都有边的最大点集的大小. ...
- 回溯法——最大团问题(Maximum Clique Problem, MCP)
概述: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题.最大团问题又称为最大独立集问题(Maximum Independent ...
- codeforces 442B B. Andrey and Problem(贪心)
题目链接: B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input ...
随机推荐
- Android中使用画笔和画布绘制一个矩形
场景 在Android中画笔使用Paint类,画布使用Canvas类来表示. 绘图的基本步骤 首先编写一个继承自View的自定义View类,然后重写其onDraw方法,最后把自定义的view添加到ac ...
- 3Python脚本在linux环境下头文件解释
#!/usr/bin/python到底是什么意思 有这句的,加上执行权限后,可以直接用 ./ 执行,不然会出错,因为找不到 python 解释器. #!/usr/bin/python 是告诉操作系统执 ...
- 5种PHP生成图片验证码实例
5种PHP生成图片验证码实例,包括数字验证码.数字+字母验证码.中文验证码.仿google验证码和算术验证码,PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session ...
- textarea输入文字限制个数
说明: w-count固定为数字部分的class textarea-active为超出最大输入文字个数报错信息的class html 部分: <div class="wrap wrap ...
- vue 注意
Path Intellisense 插件 @路径提醒 配置文件中配置: "path-intellisense.mappings": { "@": "$ ...
- cf877D
题意简述:矩阵中有的点不能走,你每次可从四个方向走,至少走一步,最多走k步(不能横跨那些不能走的格子),问从(sx,sy)走到(tx,ty)的最短时间是多少? 题意:利用set来加速bfs的过程,原理 ...
- C# MVC 全局错误Application_Error中处理(包括Ajax请求)
在MVC的Global.asax Application_Error 中处理全局错误. 如果在未到创建请求对象时报错,此时 Context.Handler == null . 判断为Ajax请求时,我 ...
- linux--工具参考篇
linux下的好用工具 工具好多!!! gdb调试利器 ldd查看程序依赖库 lsof一切皆文件 ps进程查看器 pstack 跟踪进程栈 strace 跟踪进程中的系统调用 ipcs 查询进程间通信 ...
- docker常见操作总结
一.原理 1.Hypervisor是一种运行在物理服务器和操作系统之间的中间软件层,可允许多个操作系统和应用共享一套基础物理硬件,它能直接访问物理设备,会给每一台虚拟机分配内存.CPU.网络.磁盘等资 ...
- pip 配置国内源
网上已经有很多人介绍了,我写在这儿就是为了找起来方便. https://www.cnblogs.com/schut/p/10410087.html 这篇博客还是很有意思的,他介绍了两种方法.可以一 ...