跳到正文

题解

题解 【AT4353 [ARC101D] Robots and Exits】

DreamsChaser约 6 分钟
原文
文章信息与版本

文章信息

发布
2021年9月30日
收录
2026年7月16日
更新
2026年11月16日
篇幅
约 6 分钟
获赞
31

版本

  1. v1当前

更好的阅读体验:https://www.cnblogs.com/DreamsChaser/p/15272849.html

Description\large\mathcal{Description}

在一个数轴上有 nn 个 robot\texttt{robot} 和 mm 个 exit\texttt{exit}.

每次操作可以把所有 robot\texttt{robot} 同时向左或向右移动 11 个单位长度,当一个 robot\texttt{robot} 遇到了一个 exit\texttt{exit}, 它就会溜走。所有 robot\texttt{robot} 溜走之后停止操作。

求 robot\texttt{robot} 从 exit\texttt{exit} 溜走的方案数。两个方案不同,当且仅当存在至少一个 robot\texttt{robot} 从不同的 exit\texttt{exit} 溜走。

答案对 109+710^9+7 取模,n,m≤105.n, m\le 10^5.

Solution\large\mathcal{Solution}

首先我们可以不管比最左边 exit\texttt{exit} 还左边的 robot\texttt{robot}, 因为它一定从最左边 exit\texttt{exit} 溜走,对答案没有影响。同理,我们也可以不管比最右边 exit\texttt{exit} 还右边的 robot\texttt{robot}.

我们现在单独拎出一个 robot\texttt{robot} 进行研究。我们发现只有改变他向左或向右的最大值的操作才对这个 robot\texttt{robot} 有效果。所以我们如果把这个 robot\texttt{robot} 向左和向右的最大值作为一个平面坐标的两维的话,它的初始值是 (0,0)(0, 0), 然后不断地向上、向右延伸。

其实所有 robot\texttt{robot} 的曲线都长一个样对吧。

那一个 robot\texttt{robot} 何时溜走呢?是不是当他碰到了一个 exit\texttt{exit} 的时候。也就是说,我们设初始时第 ii 个 robot\texttt{robot} 距离离它最近的两边的 exit\texttt{exit} 的距离分别是 ai,bia_i, b_i. 然后这条曲线碰到 x=aix=a_i 或 y=biy=b_i 的时候它就推出历史的舞台了。

而且答案只取决于曲线先碰到 x=aix=a_i 还是 y=biy=b_i.

如果将 robot\texttt{robot} 的左标定义为 (ai−0.5,bi−0.5)(a_i-0.5, b_i-0.5). 那么所有在曲线左上方的 robot\texttt{robot} 都会从左侧的 exit\texttt{exit} 溜走,在曲线右下方的 robot\texttt{robot} 都会从右侧的 exit\texttt{exit} 溜走。

对于每个从右侧 exit\texttt{exit} 溜走的 robot\texttt{robot} 的集合, 容易构造出与之唯一对应的折线:

感性理解一下,就是卡在每个 robot\texttt{robot} (称为 BR\texttt{BR})的直线。反之如果不能这样构造直线,我们也可以保证不存在直线使得这些 robot\texttt{robot} 都在右下方。

那么方案数就等于这样的直线的个数。令 fif_i 表示延伸到第 ii 个机器人且这是个 BR\texttt{BR}. 转移方程显然:

fi=∑aj<ai, bj<bifj+1f_i=\sum_{a_j<a_i,\ b_j<b_i}f_j+1

然后这是一个 O(n2)\mathcal{O}(n^2) 的转移式,显然是个二维偏序问题,用树状数组将其优化成 O(nlog⁡n)\mathcal{O}(n\log n).

Code\large\mathcal{Code}

C++
const int N = 100010, mod = 1000000007;

int n, m, a[N], b[N], Max, dc[N];
pair<int, int> node[N];

struct BIT // 树状数组
{
    int c[N];
    void add(reg int x, reg int d) {for (; x <= Max; x += (x & (-x))) c[x] = (c[x] + d) % mod;}
    int ask(reg int x) {reg int sum = 0; for (; x; x -= (x & (-x))) sum = (sum + c[x]) % mod; return sum;}
} S;

int main()
{
    scanf("%d %d", &n, &m);
    for (reg int i = 1; i <= n; ++ i) scanf("%d", &a[i]);
    for (reg int i = 1; i <= m; ++ i) scanf("%d", &b[i]);
    
    reg int tot = 0;
    for (reg int i = 1; i <= n; ++ i)
    {
        reg int pos = lower_bound(b + 1, b + m + 1, a[i]) - b;
        if (pos == 1 || pos > m) continue;
        node[ ++ tot] = make_pair(a[i] - b[pos - 1], - (b[pos] - a[i]));
        dc[tot] = -node[tot].second;
    }

    // 离散化
    
    sort(node + 1, node + tot + 1);
    for (reg int i = 1; i <= n; ++ i) node[i].second *= -1;
    sort(dc + 1, dc + tot + 1);
    Max = unique(dc + 1, dc + tot + 1) - dc;
    for (reg int i = 1; i <= tot; ++ i) node[i].second = lower_bound(dc + 1, dc + Max, node[i].second) - dc + 1;

    // DP
    
    S.add(1, 1);
    for (reg int i = 1; i <= tot; ++ i)
    {
        if (node[i].first == node[i - 1].first && node[i].second == node[i - 1].second) continue;
        reg int t = S.ask(node[i].second - 1);
        S.add(node[i].second, t);
    }
    
    printf("%d\n", S.ask(Max));
    
    return 0;
}