zoukankan      html  css  js  c++  java
  • [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
    ", Ans);
    	return 0;
    }
    

      

  • 相关阅读:
    EFCore数据库迁移命令
    EF基本操作
    EF执行存储过程
    [vue]element-ui使用
    [vue]vue-router的使用
    [vue]使用webpack打包
    [vue]插槽与自定义事件
    [vue]计算属性
    [vue]axios异步通信
    [vue]组件
  • 原文地址:https://www.cnblogs.com/JoeFan/p/4347465.html
Copyright © 2011-2022 走看看