【loj6307】「雅礼国庆 2017 Day1」Clique 贪心

题目描述

数轴上有 $n$ 个点,第 $i$ 个点的坐标为 $x_i$ 权值为 $w_i$ 。两个点 $i,j$ 之间存在一条边当且仅当 $|x_i−x_j|le w_i+w_j$ 。

你需要求出这张图的最大团的点数。(团就是两两之间有边的顶点集合)

$nle 2 imes 10^5$ 。


题解

贪心傻逼题

把绝对值展开后得到 $x_i+w_ile x_j-w_j , x_i<x_j$ ,等价于:每个点相当于 $[x_i-w_i,x_i+w_i)$ 这段区间,两个点之间有边当且仅当对应区间不相交。

问题转化为给出数轴上的 $n$ 个区间,求最多选出多少个,使得它们两两不相交。按右端点排序,贪心地能选择选即可。

时间复杂度为排序的 $O(nlog n)$ 

#include <cstdio>
#include <algorithm>
#define N 200010
using namespace std;
struct data
{
	int l , r;
	bool operator<(const data &a)const {return r < a.r;}
}a[N];
int f[N];
int main()
{
	int n , i , x , y , last = 0x80000000 , ans = 0;
	scanf("%d" , &n);
	for(i = 1 ; i <= n ; i ++ ) scanf("%d%d" , &x , &y) , a[i].l = x - y , a[i].r = x + y;
	sort(a + 1 , a + n + 1);
	for(i = 1 ; i <= n ; i ++ )
		if(last <= a[i].l)
			last = a[i].r , ans ++ ;
	printf("%d
" , ans);
	return 0;
}
原文地址:https://www.cnblogs.com/GXZlegend/p/8625959.html