2020杭电第四场1008

论矿工的自我修养

这道题呢,题意是这样的,给你一个网格图(n*m),这个网格上面有k个地雷。有一个矿工从左上角的(1,1)出发,然后他只能往右或者王下走,不能踩到地雷,请问他能踩到的格子数量有多少个。

题解,我们考虑对于每一列,已经存在的走不到的区间的集合为((l1,r1),(l2,r2),(l3,r3)…),保证所有的区间不相交,初始化的时候将第一个区间预处理出来即可。
此时,对于下一列中的每一个点,存在两种情况,一种是(li-1<=deep<=ri+1),一种是在这个外面,因此,我们只要找到对于每一个点来说的前一种情况并且将区间(deep,max(deep,ri+1))更新到下一个区间之中就可以了。此时所有的这些区间有可能有交集,为了保证下一次运算的时候不出错,我们必须求一个区间和并,利用一个单调队列即可。
注意,我们初始的时候必须要将区间(0,0)加入队列,并且在每一列中同样加入0这个初始点,这是为了初始化每列用的,与初始化第一个区间时同样的理由。

以下为ac代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
priority_queue<P, vector<P>, greater<P>> que, tp;
priority_queue<P> temp;
vector<ll> g[111111];
int main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        ll n, m, k;
        cin >> n >> m >> k;
        for (ll i = 1; i <= n; i++)
            g[i].clear();
        while (!que.empty())
            que.pop();
        while (!tp.empty())
            tp.pop();
        while (k--)
        {
            ll x, y;
            cin >> x >> y;
            g[x].push_back(y);
        }
        for (ll i = 2; i <= n; i++)
            g[i].push_back(0);
        for (ll i = 1; i <= n; i++)
            sort(g[i].begin(), g[i].end());
        ll res = m * n;
        if (g[1].size())
        {
            if (g[1][0] > 1)
                que.push(P(g[1][0], m)), que.push(P(0, 0)), res -= m - g[1][0] + 1;
            else
                que.push(P(0, m)), res -= m;
        }
        else
            que.push(P(0, 0));
        for (ll i = 2; i <= n; i++)
        {
            res++;
            P pre = que.top();
            que.pop();

            for (auto it : g[i])
            {
                int cnt = 0;
                while (1)
                {
                    P now = pre;
                    //magic code,don't change it if you can't understand any code in it,especailly the cnt and break;
                    while (!que.empty() && it - 1 >= now.second)
                    {
                        now = que.top();
                        que.pop();
                    }
                    if (it >= now.first - 1)
                    {
                        tp.push(P(it, max(it, now.second)));
                    }
                    else
                        tp.push(P(it, it));
                    pre = now;
                    if (que.empty() || que.top().first - 1 > it)
                        break;
                    if (cnt > 0)
                        break;
                    cnt++;
                }
            }
            while (!que.empty())
                que.pop();
            if (tp.empty())
                continue;
            pre = tp.top();
            res -= pre.second - pre.first + 1;
            tp.pop();
            temp.push(pre);
            while (!tp.empty())
            {
                P now = tp.top();
                tp.pop();
                if (now.first - 1 <= pre.second)
                {
                    temp.pop();
                    res += pre.second - pre.first + 1;
                    temp.push(P(pre.first, now.second));
                    pre = P(pre.first, now.second);
                    res -= pre.second - pre.first + 1;
                }
                else
                {
                    temp.push(now);
                    pre = now;
                    res -= pre.second - pre.first + 1;
                }
            }
            while (!temp.empty())
                que.push(temp.top()), temp.pop();
        }
        cout << res << '\n';
    }
}

这个代码有些丑陋,有魔法的成分在里面,请各位酌情观看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值