跳到正文

题解

题解:CF2227E It All Went Sideways

Liu0628约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年8月10日
收录
2026年8月11日
更新
2026年10月31日
篇幅
约 8 分钟
题目
CF2227E

版本

  1. v1当前

CF2227E题解

闲话

模拟赛 T1T1,没有想到单调栈,就用了一种极其猎奇的线段树写法。

题目大意

传送门。

有 nn 列方块,第 ii 列的高度为 aia_i。重力原向下,每个方块在高度 1∼ai1∼a_i 各一个。突然重力转向右,每个方块会水平向右滑动到不能动为止(不能穿过其他方块)。你可以选择至多一次操作:将某一列的高度减少 11(即移除一个方块),也可以不操作。求在最优操作下,最终移动的方块总数最大是多少。

做法讲解

先考虑如何统计答案。

将重力向右转后,有的方块会向右移动,有的则不会动。那么移动的方块的总数就等于 总的方块数-不动的方块数。考虑如何计算。可以发现,一个方块不动,当且仅当这个方块的右面有一直延伸到 nn 的方块支撑。那么就可以从后向前枚举,遇到比当前的最低高度要高的,就将其设为最低高度,否则就更新当前最低高度。

代码如下:

C++
for (int i = n; i >= 1; i--)
            if (a[i - 1] > a[i])
                res += a[i - 1] - a[i], a[i - 1] = a[i];

接着考虑操作。

可以发现我们的每次操作,都只会选择每一个高度的最靠后的位置进行操作。

证明也很简单,如果选择的不是最靠后的,那么就会有当前高度的方块不移动,那么所达到的结果就一定小于选择最靠后的位置的结果。

令 cthct_h 表示高度 hh 最后出现的位置,poshpos_h 表示最后一个高度小于 hh 的位置。

每一次操作的贡献也很好求,就是 cti−posi−1ct_i-pos_i-1,减一是因为要去掉删去的方块的贡献。那么进行操作的位置就是使得 cti−posi−1ct_i-pos_i-1 取得最大值的位置,记为 pp。然后,只需要将 acpa_{c_p} 减一,再求一遍答案,最后将两次答案求 max⁡\max,输出即可。

代码实现

ctct 数组是很容易求出的,在输入的时候覆盖位置就行。

pospos 数组可以用线段树维护 (可能有更优的,但是我不会) ,从前向后扫一遍,比较相邻两个 ai−1a_{i-1} 和 aia_i 的大小,如果 ai−1a_{i-1} 小,就把 posai−1+1pos_{a_{i-1}+1} 到 posaipos_{a_i} 赋值为 i−1i-1。具体细节看代码吧。

CF的AC记录。

C++
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <cstring>

#define r(x) x = read()
#define w(x) write(x)
#define ls p << 1
#define rs p << 1 | 1
#define int long long
using namespace std;
typedef unsigned long long ull;
const int N = 4e5 + 10;
const int D = 205;
const int Mod = 998244353;
inline int read()
{
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
inline void write(int x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
int _;
int n, a[N], pos[N], ct[N], s[N], ta[N];
int tr[N << 2], tag[N << 2];
bool f[N << 2];
void build(int p, int pl, int pr)
{
    tag[p] = -1;
    if (pl == pr)
    {
        tr[p] = 0;
        return;
    }
    int mid = pl + pr >> 1;
    build(ls, pl, mid);
    build(rs, mid + 1, pr);
}
void addtag(int p, int d)
{
    tr[p] = d;
    tag[p] = d;
}
void push_down(int p)
{
    if (tag[p] == -1)
        return;
    addtag(ls, tag[p]);
    addtag(rs, tag[p]);
    tag[p] = -1;
}
void update(int p, int pl, int pr, int L, int R, int d)
{
    if (L <= pl && pr <= R)
    {
        addtag(p, d);
        return;
    }
    push_down (p);
    int mid = pl + pr >> 1;
    if (L <= mid)
        update(ls, pl, mid, L, R, d);
    if (R > mid)
        update(rs, mid + 1, pr, L, R, d);
}
int query(int p, int pl, int pr, int x)
{
    if (pl == pr)
        return tr[p];
    push_down(p);
    int mid = pl + pr >> 1;
    if (x <= mid)
        return query(ls, pl, mid, x);
    else
        return query(rs, mid + 1, pr, x);
}
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> _;
    while (_--)
    {
        cin >> n;
        int mi = 0x7fffffffffffffff, ans = 0;
        a[0] = ta[0] = 0;
        for (int i = 1; i <= n; i++)
            ct[i] = 0, s[i] = 0, pos[i] = n + 1;
        for (int i = 1; i <= n; i++)
            cin >> a[i], ct[a[i]] = i, ta[i] = a[i];
        build(1, 1, n + 1);
        for (int i = n; i >= 1; i--)
            if (ta[i - 1] > ta[i])
                ans += ta[i - 1] - ta[i], ta[i - 1] = ta[i];
        a[n + 1] = n + 1;
        for (int i = 1; i <= n + 1; i++)
            if (a[i] > a[i - 1])
            {
                if (i - 1 == 0)
                    continue;
                update(1, 1, n + 1, a[i - 1] + 1, a[i], i - 1);
            }
        for (int i = 1; i <= n; i++)
            pos[i] = query(1, 1, n + 1, i);
        int p = 0;
        for (int i = 1; i <= n; i++)
            if (ct[i] - pos[i] - 1 > ct[p] - pos[p] - 1)
                p = i;
        a[ct[p]]--;
        int res = 0;
        for (int i = n; i >= 1; i--)
            if (a[i - 1] > a[i])
                res += a[i - 1] - a[i], a[i - 1] = a[i];
        cout << max(res, ans) << "\n";
    }
    return 0;
}
原文