hdu 3511 圆扫描线

hdu3511

题意

给n(50,000)个坐标系上的圆,xi,yi,ri。
这些圆相互不相交,并且不相切,只有相互无关或者包含的关系。
(这句话保证了扫描线的可行性)。
求被最深处的圆被其他圆包含了几次。

解析

扫描线学习心得
这个博客写的好,通俗易懂。
用set来维护所谓的上下事件点的关系。
优先级别高的处在前面位置。
线的优先级别的确定是根据y点的大小来判断的,y越大,优先级越高。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 50000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
//
const int UP = 0;
const int DOWN = 1;
const int IN = 0;
const int OUT = 1;
//
int nowLineX;

struct Circle
{
    int x, y, r;
    int dep;

    void read()
    {
        scanf("%d%d%d", &x, &y, &r);
        dep = 0;
    }

    int getX(int tag)
    {
        if (tag == IN)
            return x - r;
        return x + r;
    }

    double getY(int tag)
    {
        double d = sqrt(r * 1.0 * r - (nowLineX - x) * 1.0 * (nowLineX - x));
        if (tag == UP)
            return y + d;
        return y - d;
    }
} circle[maxn];

int lineNum;
struct Line
{
    int x, y;
    int id;
    int tag;
    void read(int _x, int _y, int _id, int _tag)
    {
        x = _x;
        y = _y;
        id = _id;
        tag = _tag;
    }
    bool operator < (const Line &b)const
    {
        return (x < b.x) || (x == b.x && y > b.y);
    }
} line[maxn << 1];

struct Node
{
    int id;
    int tag;
    Node(){}
    Node(int _id, int _tag)
    {
        id = _id;
        tag = _tag;
    }
    bool operator < (const Node &b)const
    {
        double ya = circle[id].getY(tag);
        double yb = circle[b.id].getY(b.tag);
        return (ya > yb) || (ya == yb && tag < b.tag);
    }
};

set<Node> lines;
set<Node>::iterator st, ed, it;

void scanLine()
{
    lines.clear();
    for (int i = 0; i < lineNum; i++)
    {
        nowLineX = line[i].x;
        if (line[i].tag == OUT)
        {
            lines.erase(Node(line[i].id, UP));
            lines.erase(Node(line[i].id, DOWN));
        }
        else
        {
            ///插入完毕后,返回当前插入的这个元素的迭代器
            it = lines.insert(Node(line[i].id, UP)).first;
            st = ed = it;
            ed++;
            int id = it->id;
            //没有上方事件点,或者没有下方事件点
            if (st == lines.begin() || ed == lines.end())
            {
                circle[id].dep = 1;
            }
            //有上下方事件点
            else
            {
                st--;
                //上下方事件点属于同一个圆 包含关系
                if (st->id == ed->id)
                {
                    circle[id].dep = circle[st->id].dep + 1;
                }
                //上下方事件点不属于同一个圆
                //情况1:取上下方事件点深度大的
                //情况2:上下方事件点深度相同
                else
                {
                    circle[id].dep = max(circle[st->id].dep, circle[ed->id].dep);
                }
            }
            lines.insert(Node(line[i].id, DOWN));
        }
    }
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    int n;
    while (~scanf("%d", &n))
    {
        lineNum = 0;
        for (int i = 0; i < n; i++)
        {
            circle[i].read();
            line[lineNum++].read(circle[i].getX(IN), circle[i].y, i, IN);
            line[lineNum++].read(circle[i].getX(OUT), circle[i].y, i, OUT);
        }
        sort(line, line + lineNum);
        scanLine();
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            ans = max(ans, circle[i].dep);
        }
        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值