zoukankan      html  css  js  c++  java
  • [CF35E] Parade

    [CF35E] Parade - 扫描线

    Description

    给出 n 个底边在 x 轴上的矩形,求外面的轮廓线顶点。

    Solution

    考虑扫描线,用一个 multiset 维护当前穿过的所有矩形

    一个矩形转化为两个事件:添加一个数和删除一个数

    如果操作完后集合的 max 和操作之前不同,那么它就会贡献两个顶点

    #include <bits/stdc++.h>
    using namespace std;
    
    signed main()
    {
        ios::sync_with_stdio(false);
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        int n;
        cin >> n;
        vector<pair<int, int>> vec;
        for (int i = 0; i < n; i++)
        {
            int h, x, y;
            cin >> h >> x >> y;
            vec.push_back({x, h});
            vec.push_back({y, -h});
        }
        sort(vec.begin(), vec.end());
        vector<pair<int, int>> ans;
        multiset<int> ms;
        ms.insert(0);
        for (int l = 0; l < vec.size();)
        {
            int r = l;
            int h_o = *ms.rbegin();
            while (r < vec.size() && vec[l].first == vec[r].first)
            {
                auto [x, h] = vec[r];
                if (h > 0)
                    ms.insert(h);
                else
                    ms.erase(ms.find(-h));
                ++r;
                if (r > 2 * n)
                    break;
            }
            int h_n = *ms.rbegin();
            if (h_o != h_n)
            {
                ans.push_back({vec[l].first, h_o});
                ans.push_back({vec[l].first, h_n});
            }
            l = r;
        }
        cout << ans.size() << endl;
        for (auto [x, y] : ans)
            cout << x << " " << y << endl;
    }
    
  • 相关阅读:
    构建之法第九、十章读后感
    构建之法第七章读后感
    构建之法五、六章读后感
    构建之法第四章读后感
    一组阶段小记之再读构建之法
    打印控件
    TTS语音
    VS2005通过网络连接CE设备进行调试开发
    Windows XP下安装WinCE6.0开发环境
    利用VS2005创建WINCE 6.0 平台
  • 原文地址:https://www.cnblogs.com/mollnn/p/14412089.html
Copyright © 2011-2022 走看看