[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 ...
随机推荐
- MP算法和OMP算法及其思想
主要介绍MP(Matching Pursuits)算法和OMP(Orthogonal Matching Pursuit)算法[1],这两个算法尽管在90年代初就提出来了,但作为经典的算法,国内文献(可 ...
- Linux下jvm、tomcat、mysql、log4j优化配置笔记[转]
小菜一直对操作系统心存畏惧,以前也很少接触,这次创业购买了Linux云主机,由于木有人帮忙,只能自己动手优化服务器了.... 小菜的云主机配置大致为:centeos6(32位),4核心cpu,4G内存 ...
- memcpy的使用方法总结
1.memcpy 函数用于 把资源内存(src所指向的内存区域) 复制到目标内存(dest所指向的内存区域):拷贝多少个?有一个size变量控制拷贝的字节数:函数原型:void *memcpy(voi ...
- redis持久化和常见故障
https://segmentfault.com/a/1190000004135982 redis 主从复制 Redis主从复制的原理 当建立主从关系时,slave配置slaveof <mast ...
- careercup-栈与队列 3.5
3.5 实现一个MyQueue类,该类用两个栈来实现一个队列. 解答 队列是先进先出的数据结构(FIFO),栈是先进后出的数据结构(FILO), 用两个栈来实现队列的最简单方式是:进入队列则往第一个栈 ...
- WTL 自绘 进度条Progressbar
WTL 绘制的进度条,逻辑清晰明了,代码函数清晰易懂:基本思路就是 首先绘制 进度条背景图,然后根据动态进度不断重绘前景进度条,绘制操作在OnPaint函数里画.该类可以直接用于项目中. 使用示例: ...
- mybatis缓存清除方法
String cacheName = IWenshiduDao.class.getName(); Ehcache cache = CacheManager.create().getEhcache(ca ...
- NET/ASP.NET Routing路由(深入解析路由系统架构原理)(转载)
NET/ASP.NET Routing路由(深入解析路由系统架构原理) 阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模 ...
- php5魔术函数、魔术常量
魔术函数 1.__construct() 实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用. 2.__destruct ...
- javascript创建对象的7种方式
/*1.工厂模式*/ function createPerson(name,age,job) { var o = new object(); o.name = name; o.age = age; o ...