POJ 2932 Coneology (平面扫描)

Coneology

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3713   Accepted: 720

Description

A student named Round Square loved to play with cones. He would arrange cones with different base radii arbitrarily on the floor and would admire the intrinsic beauty of the arrangement. The student even began theorizing about how some cones dominate other
cones: a cone A dominates another cone B when cone B is completely within the cone A. Furthermore, he noted that there are some cones that not only dominate others, but are themselves dominated, thus creating complex domination
relations. After studying the intricate relations of the cones in more depth, the student reached an important conclusion: there exist some cones, all-powerful cones, that have unique properties: an all-powerful cone is not dominated by any other
cone. The student became so impressed by the mightiness of the all-powerful cones that he decided to worship these all-powerful cones.

Unfortunately, after having arranged a huge number of cones and having worked hard on developing this grandiose cone theory, the student become quite confused with all these cones, and he now fears that he might worship the wrong cones (what if there is
an evil cone that tries to trick the student into worshiping it?). You need to help this student by finding the cones he should worship.

Input

The input le specifies an arrangement of the cones. There are in total N cones (1 ≤ N ≤ 40000). Cone i has radius and height equal to Rii = 1 … N. Each cone is hollow on the inside and has
no base, so it can be placed over another cone with smaller radius. No two cones touch.

The first line of the input contains the integer N. The next N lines each contain three real numbers Rixiyi separated by spaces, where (xiyi)
are the coordinates of the center of the base of cone i.

Output

The first line of the output le should contain the number of cones that the student should worship. The second line contains the indices of the cones that the student should worship in increasing order. Two consecutive numbers should be separated by a single
space.

Sample Input

5
1 0 -2
3 0 3
10 0 0
1 0 1.5
10 50 50

Sample Output

2
3 5

题意:

平面上有N个两两没有公共点的圆,i号圆的圆心在(xi,yi),半径为ri。求所有最外层的,即不包含于其他圆内部的圆。

分析:

在几何问题中,我们经常利用平面扫描技术来降低算法的复杂度。所谓平面扫描,是指扫描线在平面上按给定的轨迹移动的同时,不断根据扫描线扫过的部分更新信息,从而得到整体所要求得结果的方法。扫描的方法,既可以从左向右平移与y轴平行的直线,也可以固定射线的端点逆时针转动。

对于这道题,我们从左向右平移与y轴平行的直线的同时,维护与扫描线相交的最外层的圆的集合。从左向右移动的过程中,只有扫描线移动到圆的左右两端时,圆与扫描线的相交关系才会发生变化,因此我们先将所有的这样的x坐标枚举出来并排好序。

首先,我们来看一下扫描线移动到某个圆的左端时的情况。此时,我们需要判断该圆是否包含在其他圆中。为此,我们只需从当前与扫描线相交的最外层的圆中,找到上下两侧y坐标方向距离最近的两个圆,并检查他们就足够了。这是因为,假设该圆被包含于更远的圆中,却不被包含于最近的圆中,就会如下图所示,得出两圆相交的结论。而这与题目所给的条件不符。于是,只要用二叉查找树来维护这些圆,就能够在O(logn)时间内取得待检查的圆了。

其次,我们看一下扫描线移动到某个圆的右端时的情形。此时的处理很简单,如果该圆已经包含于其他圆中就什么也不做,如果是最外层的圆就将它从二叉树中删去。综上,总的复杂度为O(nlogn)。

#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
const int maxn = 40000 + 10;

int n;
double x[maxn], y[maxn], r[maxn];

//判断圆i是否在圆j的内部
bool inside(int i, int j)
{
    double dx = x[i] - x[j];
    double dy = y[i] - y[j];
    return dx * dx + dy * dy <= r[j] * r[j];
}

void solve()
{
    //枚举关键点
    vector<pair<double, int> > events;   //圆的左右两端的x坐标
    for (int i = 0; i < n; i++){
        events.push_back(make_pair(x[i] - r[i], i));    //圆的左端
        events.push_back(make_pair(x[i] + r[i], i + n));    //圆的右端
    }
    sort(events.begin(), events.end());

    //平面扫描
    set<pair<double, int> > outers;   //与扫描线相交的最外层的圆的集合
    vector<int> res;     //最外层圆的列表
    for (int i = 0; i < events.size(); i++){
        int id = events[i].second % n;
        if (events[i].second < n){        //扫描到左端
            set<pair<double, int> > :: iterator it = outers.lower_bound(make_pair(y[id], id));
            if (it != outers.end() && inside(id, it -> second))
                continue;
            if (it != outers.begin() && inside(id, (--it) -> second))
                continue;
            res.push_back(id);
            outers.insert(make_pair(y[id], id));
        }
        else{        //扫描到右端
            outers.erase(make_pair(y[id], id));
        }
    }

    sort(res.begin(), res.end());
    printf("%d\n", res.size());
    for (int i = 0; i < res.size(); i++){
        printf("%d%c", res[i] + 1, i + 1 == res.size() ? '\n' : ' ');
    }
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++){
        scanf("%lf%lf%lf", &r[i], &x[i], &y[i]);
    }
    solve();
    return 0;
}

时间: 2024-11-09 03:04:40

POJ 2932 Coneology (平面扫描)的相关文章

POJ 2932 Coneology(扫描线)

[题目链接] http://poj.org/problem?id=2932 [题目大意] 给出N个两两没有公共点的圆,求所有不包含于其它圆内部的圆 [题解] 我们计算出所有点在圆心所有y位置的x值, 由于两两没有公共点,所以当我们对所有的x坐标进行扫描时,只要扫描相邻的x, 判定扫描到的圆和与其x相邻的圆是否是圆包含关系就可以判断一个圆的位置 扫描到左端点时,如果圆其被包含,则什么都不做,否则,将这个圆加入set中, 当扫描到右端点时,如果圆被包含,则什么都不做,否则将圆从set中去除. [代码

poj 2932 Coneology(扫描线+set)

Coneology Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3574   Accepted: 680 Description A student named Round Square loved to play with cones. He would arrange cones with different base radii arbitrarily on the floor and would admire

poj 2932 Coneology (扫描线)

题意 平面上有N个两两不相交的圆,求全部最外层的,即不被其它圆包括的圆的个数并输出 思路 挑战程序竞赛P259页 代码 /* ********************************************** Auther: xueaohui Created Time: 2015-7-25 16:56:13 File Name : poj2932.cpp *********************************************** */ #include <iostr

poj 2932 Coneology 扫面线法

题意: 给n个无交点的圆,求这n个圆中不被其它圆包含的圆. 分析: 扫面线法,用二叉树(set+lowerbound方法)维护最外圆的集合. 代码: #include <iostream> #include <algorithm> #include <vector> #include <set> using namespace std; const int maxN=40012; double r[maxN],x[maxN],y[maxN]; int n;

POJ 2932 Coneology计算最外层圆个数

平面上有n个两两没有公共点的圆,i号圆的圆心在(xi,yi),半径为ri,编号从1开始.求所有最外层的,即不包含于其他圆内部的圆.输出符合要求的圆的个数和编号.n<=40000. (注意此题无相交相切!!!)工具:扫描线+set 中心思想:边界分左右端点,如图,当扫描线与k号圆左端点相切,之前用set维护一个y纵坐标的二叉树,那我们在二叉树中查找离k号圆纵坐标最近的上下两个圆(A,B),让k与A,B判是否内含即可,为什么是AB?假设有C点(离k远一些)包含k,但A不包含k,那么一定有A,C相交,

poj_3168 平面扫描

题目大意 给定平面上N个矩形的位置(给出矩形的左下角和右上角的坐标),这些矩形有些会有重叠,且重叠只会出现矩形的边重合全部或部分,矩形的顶点重合,而不会出现一个矩形的顶点位于另一个矩形的内部.     求出所有不重叠的矩形的个数. 题目分析 将每个矩形分成4个点,记录每个点所属的矩形id,对4*N个点进行按照x坐标和y坐标排序.     然后,沿着x方向进行扫描,对于相同的x,沿着该x坐标,从下向上找那些x坐标等于x的点的y坐标,判断该点所在矩形是否出现重合.若重合,则将该点所属的矩形记为重叠.

POJ 3714 Raid 平面最近点对

题目大意:给出两个集合的点,问这两个集合之间最近的点对的距离是多少. 思路:先要知道平面最近点对的分治算法,剩下的就简单了,只需要在更新答案的时候判断一下两个点是否属于两个集合就可以了. 分治算法总是十分神奇的. 对于平面最近点对,首先按照x坐标排序,然后递归进行分治,每次分治时,先获得分治得到的结果,然后按照这个结果来计算本区间.由于我们只需要计算答案小于这个结果的点对就行了,其中(l,mid)和(mid + 1,r)我们已经得到答案,只需要统计一个点在(l,mid),另一个点在(mid +

【转】[专题学习][计算几何]

原文地址:http://www.cnblogs.com/ch3656468/archive/2011/03/02/1969303.html 基本的叉积.点积和凸包等东西就不多说什么了,网上一搜一大堆,切一些题目基本熟悉了就差不多了. 一些基本的题目可以自己搜索,比如这个blog:http://blog.sina.com.cn/s/blog_49c5866c0100f3om.html 接下来,研究了半平面交,思想方法看07年朱泽园的国家队论文,模板代码参考自我校大牛韬哥: http://www.o

与平面和空间打交道的计算几何

计算几何基础 Jack Straws(POJ 1127) 原题如下: Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5494 Accepted: 2504 Description In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try