527D.Clique Problem
题解:
水题
两种做法:
1.我的
我们假设$xi>xj$ 那么拆开绝对值
$$xi-w[i]>x[j]+w[j]$$
由于$w[i]>0$,所以$x[i]+w[i]>x[j]+w[j]$
然后我们只需要在线段树上查询一段的最大值然后再单点取max就行了
#include <bits/stdc++.h>
using namespace std;
#define rint register int
#define IL inline
#define rep(i,h,t) for (int i=h;i<=t;i++)
#define dep(i,t,h) for (itn i=t;i>=h;i--)
#define me(x) memset(x,0,sizeof(x))
#define ll long long
#define mid (h+((t-h)>>1))
namespace IO
{
char ss[<<],*A=ss,*B=ss;
IL char gc()
{
return A==B&&(B=(A=ss)+fread(ss,,<<,stdin),A==B)?EOF:*A++;
}
template<class T>void read(T &x)
{
rint f=,c; while (c=gc(),c<||c>) if (c=='-') f=-; x=(c^);
while (c=gc(),c>&&c<) x=(x<<)+(x<<)+(c^); x*=f;
}
char sr[<<],z[]; int Z,C=-;
template<class T>void wer(T x)
{
if (x<) sr[++C]='-',x=-x;
while (z[++Z]=x%+,x/=);
while (sr[++C]=z[Z],--Z);
}
IL void wer1()
{
sr[++C]=' ';
}
IL void wer2()
{
sr[++C]='\n';
}
template<class T>IL void maxa(T &x,T y) { if (x<y) x=y; }
template<class T>IL void mina(T &x,T y) { if (x>y) x=y; }
template<class T>IL T MAX(T x,T y) {return x>y?x:y;}
template<class T>IL T MIN(T x,T y) {return x<y?x:y;}
};
using namespace IO;
const int N=3e5;
const int N1=8e6;
const int M=2e9;
struct re{
int x,w;
}a[N];
int rt,n;
bool cmp(re x,re y)
{
return x.x<y.x;
}
struct sgt{
int cnt,ls[N1],rs[N1],v[N1];
void change(int &x,int h,int t,int pos,int k)
{
if (!x) x=++cnt;
maxa(v[x],k);
if (h==t) return;
if (pos<=mid) change(ls[x],h,mid,pos,k);
else change(rs[x],mid+,t,pos,k);
}
int query(int x,int h,int t,int h1,int t1)
{
if (h1<=h&&t<=t1) return v[x];
int ans=;
if (h1<=mid) maxa(ans,query(ls[x],h,mid,h1,t1));
if (mid<t1) maxa(ans,query(rs[x],mid+,t,h1,t1));
return ans;
}
}S;
int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
read(n);
rep(i,,n)
{
read(a[i].x); read(a[i].w);
}
sort(a+,a+n+,cmp);
rep(i,,n)
{
int k=a[i].x-a[i].w;
int ans=;
if (k>) ans=S.query(rt,,M,,k);
S.change(rt,,M,a[i].w+a[i].x,ans+);
}
cout<<S.v[]<<endl;
return ;
}
2.题解的做法
像这种题目比较容易想到用贪心去解决
我们把绝对值等价一下
$$w[j]+x[j]<=x[i]-w[i] \ \ \ \ w[i]+x[i]<=x[j]-w[j] $$两个满足一个即可
而我们发现对任意两个用这个不等式等价于所有$[x[i]-w[i],x[i]+w[i]]$区间都不想相交
这样我们只需要按照右端点排序贪心取就可以了
527D.Clique Problem的更多相关文章
- CodeForces - 527D Clique Problem (图,贪心)
Description The clique problem is one of the most well-known NP-complete problems. Under some simpli ...
- Codeforces 527D Clique Problem
http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...
- 527D Clique Problem 判断一维线段没有两辆相交的最大线段数量
这题说的是给了n个位置 在x轴上 每个位置有一个权值为wi,然后将|xi - xj|>=wi+wj ,满足这个条件的点建一条边,计算着整张图中有多少多少个点构成的子图,使得这个子图的节点数尽量的 ...
- 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 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, 问你两两之间都有边的最大点集的大小. ...
- 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 ...
- B. Clique Problem(贪心)
题目链接: B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 回溯法——最大团问题(Maximum Clique Problem, MCP)
概述: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题.最大团问题又称为最大独立集问题(Maximum Independent ...
随机推荐
- Apache Flink:特性、概念、组件栈、架构及原理分析
2016-04-30 22:24:39 Yanjun Apache Flink是一个面向分布式数据流处理和批量数据处理的开源计算平台,它能够基于同一个Flink运行时(Flink Runtim ...
- 配置sudo记录日志
sudosudo命令用来以其他身份来执行命令,预设的身份为root.在/etc/sudoers中设置了可执行sudo指令的用户.若其未经授权的用户企图使用sudo,则会发出警告的邮件给管理员.用户使用 ...
- web.xml:<url-pattern>
web.xml 中的 <url-pattern> 是 <servlet-mapping> 或 <filter-mapping> 下的子标签. url :http:/ ...
- Mac版 IntelliJ Idea使用系列(一)
当连续import同一个包的多个类时,Idea会自动改成import xxx.*; 办法:修改Names count to use static import with '*'
- Eclipse之JSP页面的使用
Eclipse之JSP页面的使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用Eclipse创建JSP文件 1>.点击new file,选择jsp File 2&g ...
- 个人认为一个比较完整,基于tp5平台,可快速开发的B2C平台
现在全民创业,做电商的人很多,大大小小的技术公司也很多,报价也很奇葩,从100到十万的都有,把人都能转晕,有朋友问我,你们创业是不是很赚钱,其实,外行不知道内行的苦,创业控制成本第一,活下来才能赚钱. ...
- [简洁]JavaScript中添加、移除、移动、复制、创建和查找节点元素
查找: document.getElementsByTagName通过标签名获取元素,不论有多少个都返回元素集合. document.getElementsByClassName通过类名获取元素,同上 ...
- ue4 材质表达式分类
绿色节点 颜色 Color Desaturation 数学 Math GO 字体 Font FontSample,FontSampleParameter 实用程序 Utility 常用: Desatu ...
- 激活函数——tanh函数(理解)
0 - 定义 $tanh$是双曲函数中的一个,$tanh()$为双曲正切.在数学中,双曲正切“$tanh$”是由基本双曲函数双曲正弦和双曲余弦推导而来. $$tanhx=\frac{sinhx}{co ...
- monkeyrunner环境配置
1.安装JDK.android SDK.Python (安装完成后,配置环境变量:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%ANDROID_HOME%\platfor ...