[Codeforces Round #296 div2 D] Clique Problem 【线段树+DP】
题目链接:CF - R296 - d2 - D
题目大意
一个特殊的图,一些数轴上的点,每个点有一个坐标 X,有一个权值 W,两点 (i, j) 之间有边当且仅当 |Xi - Xj| >= Wi + Wj。
求这个图的最大团。
图的点数 n <= 10^5.
题目分析
两点之间右边满足 Xj - Xi >= Wi + Wj (Xi < Xj) ==> Xj - Wj >= Xi + Wi (Xi < Xj)
按照坐标 x 从小到大将点排序。用 F[i] 表示前 i 个点的最大团大小。
那么 F[i] = max(F[k]) + 1 (k < i && (Xi - Wi >= Xk + Wk))
这个前缀最大值查询用线段树实现,然后求出的 F[i] 也要存入线段树的 Xi + Wi 处。
代码
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm> using namespace std; const int MaxN = 200000 + 5, MaxNode = 200000 * 32 + 15, INF = 1000000000; int n, Index, Root, Ans;
int T[MaxNode], Son[MaxNode][2]; struct ES
{
int x, w;
bool operator < (const ES b) const
{
return x < b.x;
}
} E[MaxN]; inline int gmax(int a, int b) {return a > b ? a : b;} void Set_Max(int &x, int s, int t, int Pos, int Num)
{
if (x == 0) x = ++Index;
T[x] = gmax(T[x], Num);
if (s == t) return;
int m = (s + t) >> 1;
if (Pos <= m) Set_Max(Son[x][0], s, m, Pos, Num);
else Set_Max(Son[x][1], m + 1, t, Pos, Num);
} int Get_Max(int x, int s, int t, int r)
{
if (r >= t) return T[x];
int m = (s + t) >> 1;
int ret;
ret = Get_Max(Son[x][0], s, m, r);
if (r >= m + 1) ret = gmax(ret, Get_Max(Son[x][1], m + 1, t, r));
return ret;
} int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d%d", &E[i].x, &E[i].w);
sort(E + 1, E + n + 1);
Ans = 0;
Index = 0;
int t, Fi;
for (int i = 1; i <= n; ++i)
{
t = E[i].x - E[i].w;
if (i != 1) Fi = Get_Max(Root, -INF, INF, t) + 1;
else Fi = 1;
if (Fi > Ans) Ans = Fi;
t = E[i].x + E[i].w;
Set_Max(Root, -INF, INF, t, Fi);
}
printf("%d\n", Ans);
return 0;
}
[Codeforces Round #296 div2 D] Clique Problem 【线段树+DP】的更多相关文章
- Codeforces Round #603 (Div. 2) E. Editor 线段树
E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #765 Div.1 F. Souvenirs 线段树
题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...
- Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)
F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)
题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n n个数,然后求有多少个区间[l,r] 满足 a[l]+a[r]=max([l, ...
- Codeforces Round #406 (Div. 2) D. Legacy 线段树建模+最短路
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)
Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...
随机推荐
- 为什么DropDownList的SelectedIndexChanged事件触发不了
写的还行,转来大家看看 为什么DropDownList的SelectedIndexChanged事件触发不了? 为什么设置了DropDownList的AutoPostBack="True&q ...
- PureMVC(JS版)源码解析(一):观察者模式解析
假设一种情景,在程序开发中,我们需要在某些数据变化时,其他的类做出相应,例如在游戏中,升级一件装备,会触发玩家金币数量改变,背包数据改变和冷却队列数据改变等等.我们不可能设置setInte ...
- PHP安全设置
1.register_globals(全局变量注册开关) 2.magic_quotes_gpc(魔术引号开关) 3.magic_quotes_runtime(魔术引号开关) 4.magic_quote ...
- 关于js中return false、event.preventDefault()和event.stopPropagation()
在平时项目中,如果遇到需要阻止浏览器默认行为,大家经常会用return false;和event.preventDefault()来阻止,但对它俩的区别还是有些一知半解,于是看了文档,查了些资料,在此 ...
- JVM体系结构
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/5187049.html ...
- 核心运营报表无线端数据,pv,uv相关数据,从9月1号开始就没了,为什么?
问题现象截图 核心运营报表 从获取数据的api的地址可以看出: http://data.51buy.com/json.php?biz=statistic&mod=OrderKeyData&am ...
- 利用Inltellj创建javadoc ,用jd2chm创建chm
现在有些框架都不带javadoc 就需要自己去生成,而且真正用起来还是chm的最方便,所以写篇日志记录一下 下面我就拿struts2的源码来来举个栗子 1.第一步:创建一个空的java项目,导入框架源 ...
- WindowsServer2003SP2EnterpriseEdition在Virtual上的安装
下载镜像(迅雷): http://192.168.0.101/WindowsServer2003SP2EnterpriseEdition.iso?fid=fWljwnwNgumTtRIy- *BY*a ...
- 《将博客搬至CSDN》的文章
我的CSDN地址 博客园应该以后会很少来了.
- adb shell dumpsys package 查看versionCode
adb shell dumpsys package +包名 输出可以查看包名 aapt dump xmltree xxx.apk AndroidManifest.xml 查看AndroidManife ...