题目链接: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. shell 获取当前ip

    HOST_IP=$(hostname --all-ip-addresses | awk '{print $1}')

  2. List<T>实体去重分组:

    实体去重分组: //实体类 public class Province { public string id { get; set; } public string name { get; set; ...

  3. MYSQL 源代码 学习

    http://blog.sina.com.cn/s/articlelist_1182000643_1_1.html http://blog.csdn.net/gao1738/article/detai ...

  4. SOA体系结构之基础培训教程-大纲篇

    引言: 最近受邀做了一个企业的SOA体系结构的内训,本文是内训课程的培训大纲,分享一下吧,希望大家能够喜欢.同时也想针对大纲中列出的内容对SOA架构体系做一次回顾,如果时间允许把完整的课件也想放上来共 ...

  5. Android BLE开发——Android手机与BLE终端通信初识

    蓝牙BLE官方Demo下载地址:   http://download.csdn.net/detail/lqw770737185/8116019参考博客地址:    http://www.eoeandr ...

  6. NDK开发之数组操作

    JNI把Java数组当作引用类型来处理,JNI提供了必要的函数来访问和处理Java数组. 下面一个一个来看. 1.创建数组 我们可以使用NewArray函数在原生代码中创建数组实例,其中可以是Int. ...

  7. Shell脚本编程入门(一) 分类: 学习笔记 linux ubuntu 2015-07-09 21:06 29人阅读 评论(0) 收藏

    最近在学shell,记录一下. if语句的使用: 1.判断两个参数大小 #!/bin/sh #a test about if statement a=10 b=20 if [ $a -eq $b ]; ...

  8. CentOS7安装Python3.5

    2. 安装Python的依赖包 yum -y groupinstall "Development tools" yum -y install openssl-devel sqlit ...

  9. mysql case when then else end 用法

    select case when 判断条件 then 输出结果  else 输出结果 end from table

  10. 利用jQuery npoi插件 asxh一般处理文件实现excel的下载

    最近开发的过程中遇到这么一个问题,利用ajax和ashx文件实现下载功能.发现代码调试走完之后并没有弹出下载框. 研究了一段时间之后发现解决这种问题有两种方法,1.ajax获取数据集在前台做处理实现导 ...