hdu3511 Prison Break 圆的扫描线

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3511

题目:

Prison Break

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2149    Accepted Submission(s): 681


Problem Description
To save Sara, Michael Scofield was captured by evil policemen and he was arrested in Prison again. As is known to all, nothing can stop Michael, so Prison Break continues.
The prison consists of many circular walls. These walls won't intersect or tangent to each other. 



Now Michael is arrested in one of the deepest rooms, and he wants to know how many walls he has to break at least for running out. In figure 1, Michael has to break 3 walls at least and in figure 2, 2 walls are needed.
 

 

Input
There will be multiple test cases (no more than 10) in a test data.
For each test case, the first line contains one number: n (1<=n<=50,000) indicating the total number of circular walls.
Then n lines follow, each line contains three integers x, y, r. (x,y) indicates the center of circular wall and r indicates the radius of the wall.
-100,000<=x,y<=100,000 
1 <= r <= 100,000
The input ends up with EOF.
 

 

Output
The least number of walls to break for running out.
 

 

Sample Input
3 0 0 1 0 0 2 0 0 3 3 0 0 10 5 0 1 -5 0 1
 

 

Sample Output
3 2
 

 

Source

 

思路:

  圆的扫描。

  因为圆不相交,所以扫描线扫的时候可以成矩形来理解,扫描线上下圆的关系不变。

  并且扫到一个圆k,他的上面的圆为up,下面的为dw。

  if(up==dw&&up!=0)  deep[k]=deep[up]+1;

  else if(up||dw)  deep[k]=max(deep[up],deep[dw]);

  else  deep[k]=1;

  至于是为什么的话,自己多画画就知道了。

  还有,set并没真正存圆和扫描线的交点,因为扫描线在变交点也在变。

  set中的交点是cmp时动态求的,这还有点巧妙的。

 1 #include <bits/stdc++.h>
 2 
 3 #define MP make_pair
 4 
 5 using namespace std;
 6 
 7 const double eps = 1e-8;
 8 const int N = 1e5+10;
 9 
10 int n,cnt[N];
11 int cr[N][2],r[N];
12 pair<int,int>pt[N];
13 double curx;
14 
15 struct node
16 {
17     int id,f;
18     bool operator < (const node &ta) const
19     {
20         double y1 = cr[id][1] + f * sqrt(1.0 *r[id]*r[id]-1.0*(curx-cr[id][0])*(curx-cr[id][0]));
21         double y2 = cr[ta.id][1] + ta.f * sqrt(1.0 *r[ta.id]*r[ta.id]-1.0*(curx-cr[ta.id][0])*(curx-cr[ta.id][0]));
22         if(fabs(y1-y2)<eps)
23             return f<ta.f;
24         return y1<y2;
25     }
26 };
27 set<node >st;
28 
29 int main(void)
30 {
31     int n;
32     while(~scanf("%d",&n))
33     {
34         st.clear();
35         int tot=0,ans=1;
36         for(int i=1;i<=n;i++)
37         {
38             scanf("%d%d%d",&cr[i][0],&cr[i][1],r+i);
39             pt[tot++]=MP(cr[i][0]-r[i],i);
40             pt[tot++]=MP(cr[i][0]+r[i],i-n);
41             cnt[i]=0;
42         }
43         sort(pt,pt+tot);
44         for(int i=0;i<tot;i++)
45         {
46             int k=pt[i].second,up=0,dw=0;
47             curx = pt[i].first;
48             if(k<=0)
49                 k+=n,st.erase((node){k,-1}),st.erase((node){k,1});
50             else
51             {
52                 auto it=st.insert((node){k,-1}).first;
53                 it++;
54                 if(it!=st.end())    up = it->id;
55                 it--;
56                 if(it!=st.begin())  dw = (--it)->id;
57                 if(up==dw&&up)
58                     ans=max(ans,cnt[k]=cnt[up]+1);
59                 else if(up&&dw)
60                     ans=max(ans,cnt[k]=max(cnt[up],cnt[dw]));
61                 else
62                     cnt[k]=1;
63                 st.insert((node){k,1});
64             }
65         }
66         printf("%d\n",ans);
67     }
68 
69     return 0;
70 }

 

posted @ 2017-10-15 11:25  weeping  阅读(202)  评论(0编辑  收藏  举报