Coneology(poj2932)

来源:互联网 发布:java撤销 编辑:程序博客网 时间:2024/05/21 23:00

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 Ri, i = 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 Ri, xi, yi separated by spaces, where (xi, yi) 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坐标方向距离最近的两个圆并检查他们。
代码如下

#include<iostream>#include<vector>#include<set>#include<algorithm>using namespace std;int N;double x[50005],y[50005],r[50005];bool inside(int i,int j)//判断圆i是否在圆j内{    double dx=x[i]-x[j],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;//与扫描线相交的最外层圆的集合。注意set是平衡二叉树,存入时已经排好序    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));//找到距离该圆y坐标最近的上方的圆            if(it!=outers.end()&&inside(id,it->second)) continue;//上圆            if(it!=outers.begin()&&inside(id,(--it)->second)) continue;//下圆(因为set是平衡二叉树)            res.push_back(id);            outers.insert(make_pair(y[id],id));        }        else        outers.erase(make_pair(y[id],id));    }    sort(res.begin(),res.end());    cout<<res.size()<<endl;    for(int i=0;i<res.size()-1;i++)    cout<<res[i]+1<<" ";    cout<<res[res.size()-1]+1<<endl;}int main(){    int i;    while(cin>>N)    {        for(int i=0;i<N;i++)        cin>>r[i]>>x[i]>>y[i];        solve();    }    return 0;}
原创粉丝点击