给你最多1000个圆,问画一条直线最多能与几个圆相交,相切也算。

显然临界条件是这条线是某两圆的公切线,最容易想到的就是每两两圆求出所有公切线,暴力判断一下。

可惜圆有1000个,时间复杂度太高。

网上题解的做法是枚举每个“中心圆”,求出这个圆与其他圆的切线,然后按极角排序,扫一圈。

把每条切线看成扫入线——添加一个圆,或扫出线——删除一个圆。

形象一点就是一条与中心圆相切的直线,沿着中心圆滚了一圈,当这个直线碰到其他圆时,是添加了一个圆还是删除了一个圆。

PS:这题C++交死活TLE,G++才能交过。为什么为什么为什么orz……

PS2:这题我之前少判了一种情况(就是那个A内含B的情况),导致fix调整角度区间的时候进了死循环好像,然后TLE了很长时间,去掉fix之后又RE。然后我就来回注释这附近的代码交上去各种测,错使我在错误的地方调错调了很长时间。实际上只是因为没有把那种情况判出去,导致后面的运算非法。以后要多注意这种情况。

PS3:感觉这题还有很多细节,但是我说不清楚了,直接看代码吧……

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm> using namespace std; const int MAXN = ;
const double eps = 1e-;
const double PI = acos( -1.0 ); struct Point
{
double x, y;
Point( double x = , double y = ):x(x), y(y) { }
void readPoint()
{
scanf( "%lf%lf", &x, &y );
return;
}
}; int dcmp( double x ) //控制精度
{
if ( fabs(x) < eps ) return ;
else return x < ? - : ;
} double PointDis( Point a, Point b )
{
return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
} //调整到0~2*PI区间
double fix( double ang )
{
while ( dcmp( ang ) < ) ang += 2.0*PI;
while ( dcmp( ang - PI - PI ) >= ) ang -= 2.0*PI;
return ang;
} struct Line
{
int id; //哪个圆的切线
int s; //扫入扫出线
double ang; //极角
Line() { }
Line( int id, int s, double ang ): id(id), s(s), ang(ang) { }
}; bool cmp( const Line& lhs, const Line& rhs )
{
int tmp = dcmp( lhs.ang - rhs.ang );
if ( tmp ) return tmp < ;
else return lhs.s > rhs.s;
} struct Circle
{
Point c; //圆心坐标
double r; //半径
Circle() {}
Circle( Point c, double r ): c(c), r(r) {}
Point getPoint( double theta ) //根据极角返回圆上一点的坐标
{
return Point( c.x + cos(theta)*r, c.y + sin(theta)*r );
}
void readCircle()
{
scanf("%lf%lf%lf", &c.x, &c.y, &r );
return;
}
}; //获得切线的斜率
void GetTangent( Circle& A, Circle& B, int& id, int& sum, int& LineN, Line *L )
{
double dis = PointDis( A.c, B.c );
double base = atan2( B.c.y - A.c.y, B.c.x - A.c.x ); //A内含B
if ( dcmp( A.r - B.r - dis ) > ) return; //B内含+内切A
if ( dcmp( B.r - A.r - dis ) >= )
{
++sum;
return;
}
/*
//A内切B
if ( dcmp( A.r - B.r ) > 0 && dcmp( dis - rdiff ) == 0 )
{
L[LineN++] = Line( id, 1, base - PI/2 - eps );
L[LineN++] = Line( id, -1, base - PI/2 + eps );
return;
}
*/
//外切+相交
double ang1 = asin( (B.r - A.r) / dis );
double ang2 = asin( (A.r + B.r) / dis );
if ( dcmp( A.r + B.r - dis ) >= )
{
L[LineN++] = Line( id, , fix( base - ang1 ) );
L[LineN++] = Line( id, -, fix( base + ang1 + PI ) );
return;
} //相离
L[LineN++] = Line( id, , fix( base - ang1 ) );
L[LineN++] = Line( id, -, fix( base + ang2 ) );
L[LineN++] = Line( id, , fix( base - ang2 + PI ) );
L[LineN++] = Line( id, -, fix( base + ang1 + PI ) ); return;
} bool vis[MAXN];
int solved( int cN, int LineN, Line *L )
{
int res = ;
int sum = ; memset( vis, false, sizeof(bool)*(cN+) ); for ( int i = ; i < LineN + LineN; ++i )
{
int k = i % LineN;
int id = L[k].id;
int s = L[k].s; if ( s == )
{
if ( !vis[id] )
{
vis[id] = true;
++sum;
}
}
else
{
if ( vis[id] )
{
vis[id] = false;
--sum;
}
}
if ( sum > res ) res = sum;
}
return res;
} Line L[MAXN << ];
Circle cc[MAXN];
int cN; int main()
{
//freopen( "in.txt", "r", stdin );
//freopen( "out.txt", "w", stdout );
int T, cas = ;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%d", &cN );
for ( int i = ; i < cN; ++i )
cc[i].readCircle(); int ans = ;
for ( int i = ; i < cN; ++i )
{
int sum = ;
int LineN = ;
for ( int j = ; j < cN; ++j )
{
if ( i == j ) continue;
GetTangent( cc[i], cc[j], j, sum, LineN, L );
} sort( L, L + LineN, cmp );
sum += solved( cN, LineN, L );
if ( sum > ans ) ans = sum;
} printf( "Case #%d: %d\n", ++cas, ans);
}
return ;
}

HDU 4116 Fruit Ninja ( 计算几何 + 扫描线 )的更多相关文章

  1. HDU 4116 Fruit Ninja

    http://acm.hdu.edu.cn/showproblem.php?pid=4116 题意:给N个圆,求一条直线最多能经过几个圆?(相切也算) 思路:枚举中心圆,将其他圆的切线按照极角排序,并 ...

  2. hdu 4000 Fruit Ninja 树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4000 Recently, dobby is addicted in the Fruit Ninja. ...

  3. hdu 4620 Fruit Ninja Extreme

    Fruit Ninja Extreme Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. HDU 4620 Fruit Ninja Extreme 搜索

    搜索+最优性剪枝. DFS的下一层起点应为当前选择的 i 的下一个,即DFS(i + 1)而不是DFS( cur + 1 ),cur+1代表当前起点的下一个.没想清楚,TLE到死…… #include ...

  5. hdu - 3952 Fruit Ninja(简单几何)

    思路来自于:http://www.cnblogs.com/wuyiqi/archive/2011/11/06/2238530.html 枚举两个多边形的两个点组成的直线,判断能与几个多边形相交 因为最 ...

  6. HDU 4000 Fruit Ninja 树状数组 + 计数

    给你N的一个排列,求满足:a[i] < a[k] < a[j] 并且i < j < k的三元组有多少个. 一步转化: 求出所有满足 a[i] < a[k] < a[ ...

  7. HDU 4620 Fruit Ninja Extreme(2013多校第二场 剪枝搜索)

    这题官方结题报告一直在强调不难,只要注意剪枝就行. 这题剪枝就是生命....没有最优化剪枝就跪了:如果当前连续切割数加上剩余的所有切割数没有现存的最优解多的话,不需要继续搜索了 #include &l ...

  8. hdu 4620 Fruit Ninja Extreme(状压+dfs剪枝)

    对t进行从小到大排序(要记录ID),然后直接dfs. 剪枝的话,利用A*的思想,假设之后的全部连击也不能得到更优解. 因为要回溯,而且由于每次cut 的数目不会超过10,所以需要回溯的下标可以利用一个 ...

  9. HDU 4620 Fruit Ninja Extreme 暴搜

    题目大意:题目就是描述的水果忍者. N表示以下共有 N种切水果的方式. M表示有M个水果需要你切. W表示两次连续连击之间最大的间隔时间. 然后下N行描述的是 N种切发 第一个数字C表示这种切法可以切 ...

随机推荐

  1. ajax(form)图片上传(spring)

    第一步:spring-web.xml <!--配置上传下载--> <bean id="multipartResolver" class="org.spr ...

  2. iOS之旅--隐藏(去除)导航栏底部横线

    iOS之旅--隐藏(去除)导航栏底部横线 iOS开发大部分情况下会使用到导航栏,由于我司的app导航栏需要与下面紧挨着的窗口颜色一致,导航栏底部的横线就会影响这个美观,LZ使用了以下方法.觉得不错,分 ...

  3. 《JSON笔记之三》---postman中传入json串

    1.关于如何使用postman工具,简单的介绍一下, 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等 ...

  4. Unity基础

    unity unity 3大场景 Asset Scene Component Asset :资源导入导出 右击资源,选择导出Unity包 导入可以直接将只有复制到Asset文件夹 创建场景 File- ...

  5. 从coding.net 克隆(git clone)项目代码到本地报无权限(403)错误 解决方案

    直接从coding.net (git clone)项目代码到本地时,会提示没有权限的错误,如下图: 解决方案:添加远程地址的时候带上用户名及密码即可解决,格式如下: git clone http:// ...

  6. Java实现Avl树

    Avl树即左右子树的深度[高度]相差不可超过1,所以在插入key的时候,就会出现需要旋转[更改根节点]的操作 下面是源代码: /* the define of avltree's node */ cl ...

  7. python-4函数式编程

    1-高阶函数 变量可以指向函数.   def add(x, y, f): 例如f参数为函数 编写高阶函数,就是让函数的参数能够接收别的函数. Python内建了map()和reduce()高阶函数. ...

  8. scala Actor -03

    1.对于上一篇讲解的scala的一些补充 val files = Array[String]("a.txt","b.txt","c.txt" ...

  9. Linux下添加桌面快捷方式

    这里用Ubuntu下BurpSuite举例 sudo vim /home/user/Desktop/burpsuite.desktop //burpsuite随意起名,系统会系动创建文件 文件写入 # ...

  10. The Image Gallery Revisited

    Lets apply the Best practices to the Image Gallery . /*** index.html ***/ <!DOCTYPE html> < ...