[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 ...
随机推荐
- mybatis0201 01复习
mybatis是什么? mybatis是一个持久层框架,是apache下的开源项目,前身是itbatis,是一个不完全的ORM框架(因为mybatis提供输入和输出的映射,需要程序员自己写sql语句) ...
- 查看源码Vim+Cscope
http://blog.csdn.net/huiguixian/article/details/7044869
- 网络学习笔记----01--pathping跟踪数据包路径
操作系统win7 Pathping主要用于提供有关在来源和目标之间的中间跃点处的网络滞后和网络丢失的信息. Pathping将多个回显请求消息发送到来源和目标之间的各个路由器一段时间,然后根据各个路由 ...
- Linux学习笔记总结--memcached配置
Memcached是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库检索的结果等.简单的说就是将数据调用到 ...
- Android开发之手势滑动(滑动手势监听)详解
Android开发之手势滑动(滑动手势监听)详解 在Android应用中,经常需要手势滑动操作,比如上下滑动,或左右方向滑动,处理手势滑动通常有两种方法:一种是单独实现setOnTouchListen ...
- Android(java)学习笔记175:BroadcastReceiver之 外拨电话的广播接收者
首先我们示例工程一览表如下: 1.首先我们还是买一个收音机,定义一个OutCallReceiver继承自BroadcastReceiver,onReceive()方法中定义了监听到广播,要执行的操作: ...
- 动态树 Link-Cut Trees
动态树 动态树问题, 即要求我们维护一个由若干棵子结点无序的有根树组成的森林. 要求这个数据结构支持对树的分割.合并,对某个点到它的根的路径的某些操作,以及对某个点的子树进行的某些操作. 在这里我们考 ...
- 第三篇:web之前端之JavaScript基础
前端之JavaScript基础 前端之JavaScript基础 本节内容 JS概述 JS基础语法 JS循环控制 ECMA对象 BOM对象 DOM对象 1. JS概述 1.1. javascript ...
- return与finally
当return遇到了finally,先标记return的值,然后执行finally,当finally修改了return的值,那么执行finally后,传递最后一次return的值,若finally没有 ...
- 【转】[转]order by 1是什么意思?
[转][转]order by 1是什么意思? ORDER BY 1 表示 所select 的字段按第一个字段排序 ORDER BY ASC应该没有这样写法,ORDER BY 后面不是字段就是数字, 可 ...