POJ 2932 圆扫描线

求n个圆中没有被包含的圆。模仿扫描线从左往右扫,到左边界此时如有3个交点,则有3种情况,以此判定该圆是否被离它最近的圆包含,而交点和最近的圆可以用以y高度排序的Set来维护。因此每次到左边界插入该圆,找该圆最近的两个圆(上方和下方)判断是否包含,到右边界则从Set中删除该圆。

/** @Date    : 2017-08-13 17:27:55
  * @FileName: POJ 2932 圆扫描线.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#include <math.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

int n;
struct circle
{
	double x, y, r;
	circle(){}
	circle(double _x, double _y, double _r){x = _x, y = _y, r = _r;}
};

circle c[N];
pair<double, int>p[N*2];
int ans[N];

int isinside(int a, int b)
{
	double dx = c[a].x - c[b].x;
	double dy = c[a].y - c[b].y;
	return dx * dx + dy * dy <= c[b].r * c[b].r;
}

int main()
{
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	{
		double x, y, r;
		scanf("%lf%lf%lf", &r, &x, &y);
		c[i] = circle(x, y, r);
	}
	for(int i = 0; i < n; i++)
	{
		p[i*2] = MP(c[i].x - c[i].r, i);
		p[i*2 + 1] = MP(c[i].x + c[i].r, i + n);
	}
	sort(p, p + 2 * n);
	set<pair<double, int> >q;
	int cnt = 0;
	for(int i = 0; i < 2 * n; i++)
	{
		if(p[i].se < n)
		{
			set<pair<double, int> >::iterator it;
			/*auto */it = q.lower_bound(MP(c[p[i].se].y, p[i].se));
			if(it != q.end() && isinside(p[i].se, it->se)
			|| it != q.begin()&&isinside(p[i].se, (--it)->se))
				continue;
			q.insert(MP(c[p[i].se].y, p[i].se));
			ans[cnt++] = p[i].se;
		}
		else q.erase(MP(c[p[i].se%n].y, p[i].se%n)); 
	}
	sort(ans, ans + cnt);
	printf("%d\n", cnt);
	for(int i = 0; i < cnt; i++)
		printf("%d%s", ans[i] + 1, i==cnt-1?"\n":" ");
    return 0;
}
posted @ 2017-10-18 18:45  Lweleth  阅读(389)  评论(0编辑  收藏  举报