POJ2398——Coneology(计算几何,扫描线)

来源:互联网 发布:Java web 登录实现 编辑:程序博客网 时间:2024/05/16 10:56

Coneology
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 3771 Accepted: 739

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

51 0 -23 0 310 0 01 0 1.510 50 50

Sample Output

23 5

题意:给n个圆,都不相交。问最外层的,不包括在其他圆内部的圆。

思路:因为不相交,所以判断一个小圆是不是在一个大圆里面,只要判断圆心是不是在它里头。直接暴力显然不行。

我们想象用一条扫描线。一个set保存已知的外层圆的y坐标,扫描线平行于y轴,碰到一个圆s的左端点时,用二分法在外层圆里面找到y坐标最接近的两个圆判断一下即可。为什么可行?如果说有一个更远的外层圆,内含了s,而最近的没有,那么只有两种可能,两个外层圆相交,或者为内含关系。都矛盾。具体的可以看代码。


<span style="font-size:14px;">#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <vector>#include <queue>#include <set>using namespace std;#define MAXN 50010#define INF 1000000007int n;double x[MAXN],y[MAXN],r[MAXN];//分别记录x坐标,y坐标和半径r//判断i是否在j内,因为肯定不相交,所以只要判断i的圆心是不是在j内即可bool inside(int i,int 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用y轴坐标排序    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));            //注意it实在outers里面选,也就是确定了是在最外面的圆            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;}</span>




0 0
原创粉丝点击