【算法介绍】扫描线思想
算法简介:号 · 仙蕊玲珑
这篇文章就是继二维数点里挖下的坑的扫描线内容了。对于扫描线问题,其实是非常广泛的,就像动态规划一样。他主要是提供了一个在二维平面上用平行于坐标轴的线一个一个扫描过来的思想。总归一句话:只有你想不到,没有你扫不到。
友情链接:
只要你可以按照题目的意思建立出可以通过题目的坐标系和线段,使其可以用扫描线扫出来,那么它就是可以的!
算法思想:武 · 西风长枪
首先,既然叫做扫描线,那么上面叫做扫描呢?顾名思义,扫描扫描,从下到上扫一遍就可以了,但是扫描线其实还有一个特点,扫描线。其实所谓扫描线思想,就是把问题转化成一些线段,去按照某个顺序扫描他,然后顺便处理出问题。
我们可以先给出一个扫描线的经典问题,求矩形面积并。所谓矩形面积并,就是一堆矩形叠在一起,重复覆盖的地方只算一次,当然只出现一次的也是算一次(
那么这题首先第一眼应该都是暴力,毕竟大家都没什么好的思路去做这样奇怪的题目,那么,如果是扫描线呢?他其实是可以做出这样的题目的。为什么,我们来想想矩形是什么。小学大家就学过老师的名言:点动成线,线动成面。其实矩形的本质,就是一个线段,沿着垂直于他自己的方向移动了若干距离,得到的图形。
那么我们回顾了动态建立矩形的思想。就会发现,做扫描线的过程,不也是一个一个扫描来的吗?一个矩形,其实对应的是线段 $P_1P_2$ 的一个上界和下限。那么想要用扫描线来做出这道题,我们不难有这样的猜想:
是吧,这就是所谓扫描线的思想,我们会发现,每次遇到一个矩形的下边界,我们目前扫过来的面积长度就会增加,而每次遇到一个矩阵的上边界,扫过来的面积长度就会减少。看到下面的 cnt
了没,这个计数器的作用,是看每一个 x 坐标被覆盖的次数。如果我们遇到一个下边界在对应位置 +1,上边界就 -1 的话。其实面积就变成了:整个 cnt
数组中非 0 的数量 $\times$ 和上一个边界的距离。
然后的话,其实就已经很明显了,由于不可以快速区间加和区间减,于是用线段树解决,然后就完事了。
算法实现:花 · 深林记忆
1 | // Copied from NCC79601(LuoguUid: 60258) |
例题讲解:辅 · 团团降芦
那么又到了经典的例题讲解了,早在二维数点里面我就说过扫描线可以做二维数点问题,那么我们就来看一道例题:POJ2482 - 你窗前的星星
Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remember, vividly, on the beautiful Zhuhai Campus, 4 years ago, from the moment I saw you smile, as you were walking out of the classroom and turned your head back, with the soft sunset glow shining on your rosy cheek, I knew, I knew that I was already drunk on you. Then, after several months’ observation and prying, your grace and your wisdom, your attitude to life and your aspiration for future were all strongly impressed on my memory. You were the glamorous and sunny girl whom I always dream of to share the rest of my life with. Alas, actually you were far beyond my wildest dreams and I had no idea about how to bridge that gulf between you and me. So I schemed nothing but to wait, to wait for an appropriate opportunity. Till now — the arrival of graduation, I realize I am such an idiot that one should create the opportunity and seize it instead of just waiting.
These days, having parted with friends, roommates and classmates one after another, I still cannot believe the fact that after waving hands, these familiar faces will soon vanish from our life and become no more than a memory. I will move out from school tomorrow. And you are planning to fly far far away, to pursue your future and fulfill your dreams. Perhaps we will not meet each other any more if without fate and luck. So tonight, I was wandering around your dormitory building hoping to meet you there by chance. But contradictorily, your appearance must quicken my heartbeat and my clumsy tongue might be not able to belch out a word. I cannot remember how many times I have passed your dormitory building both in Zhuhai and Guangzhou, and each time aspired to see you appear in the balcony or your silhouette that cast on the window. I cannot remember how many times this idea comes to my mind: call her out to have dinner or at least a conversation. But each time, thinking of your excellence and my commonness, the predominance of timidity over courage drove me leave silently.
Graduation, means the end of life in university, the end of these glorious, romantic years. Your lovely smile which is my original incentive to work hard and this unrequited love will be both sealed as a memory in the deep of my heart and my mind. Graduation, also means a start of new life, a footprint on the way to bright prospect. I truly hope you will be happy everyday abroad and everything goes well. Meanwhile, I will try to get out from puerility and become more sophisticated. To pursue my own love and happiness here in reality will be my ideal I never desert.
Farewell, my princess!
If someday, somewhere, we have a chance to gather, even as gray-haired man and woman, at that time, I hope we can be good friends to share this memory proudly to relight the youthful and joyful emotions. If this chance never comes, I wish I were the stars in the sky and twinkling in your window, to bless you far away, as friends, to accompany you every night, sharing the sweet dreams or going through the nightmares together.
—— 摘自 POJ2482 - Stars in Your Window 题目背景
上面只有题目背景,别妄想在上面找到任何关于题目的信息(/yx
题目意思很简单,就是让你用一个 $W\times H$ (横向长度为 W 纵向长度为 H)的矩形,框住若干个星星,每个星星有亮度,就是让你求框住星星的亮度总和最大值。
有的人就开始说了:哎呀哎呀,刚刚是矩形还可以理解,你现在框的是点哎,怎么能用扫描线呢?你好像连线段都画不出来吧~
若至。虽然,单独的点不可以找到一个矩形或者线段来代表他。但是,你可以知道一个点在某一个范围是可以被选中的,那你不是就能构造出一个矩形了吗?大概就是下面这样:
那么这个的意义是什么呢?如果我们规定了一个矩形的右上角作为对比符,以此来判定一个矩形的话,那么只要有一个矩形的右上角位于 $(x,y)\sim (x+W-1,y+H-1)$ 那么他就可以把位于 $(x,y)$ 的点框住。那么把每一个点都扩大成一个一个矩形时,如果有重叠的矩形,就意味着位于这块重叠区域的矩形可以同时覆盖两个点。
这就是这题转化的思想了,那么实现也简单了就。
规定矩形的下边界是 +c( c 为这个矩形对应星星的亮度)上边界是 -c ,那么就可以得到一堆线段等待我们扫描,然后就按照扫描线的思路模拟即可。
例题代码:漫 · 珊珊月落
1 |
|
算法实战:性 · 积极好学
查看相关算法标签喵!
参考资料(以下排名不分先后):