poj2932
作者:互联网
Coneology
Time Limit: 5000MS Memory Limit: 65536K
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
Source
Solution
题意:坐标上有n个不相交的圆,求最外层圆的index
由于数据规模,暴力超时
sweeping line
一般有两种,平移扫描,环形扫描
对于这一题,从左到右平移扫描
用一个容器维护每个圆的左右两个端点,代表扫描到圆和扫描出圆
对于扫描到的圆,判断它是否在别的圆内
只需要判断上下最近的两个圆(可画图证明,不严谨)
用一个容器维护还没扫描出的最外圆,可以排序,再查找。总时间复杂度O(nlgn)
可以选用set
当扫描到右时,把圆从set中去除,意味着扫描过了
#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
double x[40005],y[40005],r[40005];
int n;
typedef pair<double ,int> pdi;
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];
}
int main()
{
//freopen("input.txt","r",stdin);
cin>>n;
for(int i=0;i<n;i++)
scanf("%lf%lf%lf",&r[i],&x[i],&y[i]);
vector<pdi> vt; //存左右两边界
for(int i=0;i<n;i++)
{
vt.push_back(make_pair(x[i]-r[i],i));
vt.push_back(make_pair(x[i]+r[i],i+n));
}
sort(vt.begin(),vt.end());
//扫描
set<pdi> outers; //set为了排序
vector<int> res; //存放结果
for(int i=0;i<vt.size();i++)
{
int id=vt[i].second%n;
if(vt[i].second<n) //扫描到左
{
set<pdi>::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());
cout<<res.size()<<endl;
for(int i=0;i<res.size();i++)
printf("%d%c",res[i]+1,i+1==res.size()? '\n' : ' ');
return 0;
}
WRYYYYYYYYYYYYYYYY
发布了16 篇原创文章 · 获赞 0 · 访问量 238
私信
关注
标签:poj2932,cones,扫描,outers,cone,student,id 来源: https://blog.csdn.net/qq_43737697/article/details/104114531