题目链接: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】的更多相关文章

  1. 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 ...

  2. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  3. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  4. Codeforces Round #765 Div.1 F. Souvenirs 线段树

    题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...

  5. Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)

    F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...

  6. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  7. 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, ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. lucene索引并搜索mysql数据库[转]

    由于对lucene比较感兴趣,本人在网上找了点资料,终于成功地用lucene对mysql数据库进行索引创建并成功搜索,先总结如下: 首先介绍一个jdbc工具类,用于得到Connection对象: im ...

  2. qemu 的方式安装debian 模拟powerpc

    http://bbs.pediy.com/showthread.php?p=1424746http://www.ibm.com/developerworks/cn/linux/l-qemu/ 线总结下 ...

  3. SecureCRT使用教程

    Secure CRT是一款支持 SSH2.SSH1.Telnet.Telnet/SSH.Relogin.Serial.TAPI.RAW 等协议的终端仿真程序,最吸引我的是,SecureCRT 支持标签 ...

  4. Ⅴ.AngularJS的点点滴滴-- 资源和过滤

    资源ngResource(依赖ngResource模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ ...

  5. 关于Android NDK

    把解压后的ndk放在自己想放的位置 环境变量:ndk根目录添加到PATH=$PATH:<ndk-root-path> 使用NDK:在自己工作目录(可以是随意位置)下创建<Test&g ...

  6. 自定义带有图片的PreferenceActivity

    http://my.oschina.net/huangsm/blog/40027 和大家分享一下关于android中PreferenceActivity使用以及为配置信息文件中添加图标的功能,首先给大 ...

  7. DropDownList绑定多个字段值

    发觉这个问题还是挺多人问的,简单写几个例子: 假设现有1张表名为:XUDAXIA  , 该表里有2个字段:  NAME , GENDER 达到效果: 将这2个字段绑定到DropDownList的Lis ...

  8. OC - 17.AFNetworking原理及常用操作

    AFN的六大模块 NSURLConnection,主要对NSURLConnection进行了进一步的封装,包含以下核心的类: AFURLConnectionOperation AFHTTPReques ...

  9. 【html】【0】开始的序言

    人生总得做点什么才显得有意义,在牛逼的梦想也抵挡不住你傻逼似的坚持! 1>本系列适用于没有任何计算机语言基础的小白入门级教程 2>为了我喜欢的一个女生小娜娜 3>为自己系统的学习ht ...

  10. WPF 窗体中的 Canvas 限定范围拖动 鼠标滚轴改变大小

    xaml代码: <Canvas Name="movBg"> <Canvas.Background> <LinearGradientBrush EndP ...