跳到正文

题解

题解:AT_agc040_e [AGC040E] Prefix Suffix Addition

OldDriverTree约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 5 分钟

版本

  1. v1当前

好题。

Solution

首先如果只有第一种操作 / 第二种操作,那么答案就为 ∑i=1n+1[ai<ai−1]\sum\limits_{i=1}^{n+1} [a_i<a_{i-1}] / ∑i=1n+1[ai>ai−1]\sum\limits_{i=1}^{n+1} [a_i>a_{i-1}]。

然后可以发现本题就相当于将 aia_i 拆分为 pi+qip_i+q_i,最小化 ∑i=1n+1[pi<pi−1]+∑i=1n+1[qi>qi−1]\sum\limits_{i=1}^{n+1} [p_i<p_{i-1}]+\sum\limits_{i=1}^{n+1} [q_i>q_{i-1}]。然后考虑这样一个暴力 DP\text{DP},设 fx,yf_{x,y} 表示考虑前 xx 个数,px=yp_x=y 的答案最小是多少,状态转移方程就为 fx,y=min⁡i=0ax−1{fx−1,i+[y<i]+[ax−y>ax−1−i]}f_{x,y}=\min\limits_{i=0}^{a_{x-1}}\{f_{x-1,i}+[y<i]+[a_x-y>a_{x-1}-i]\}。

但是这样复杂度直接升天,继续挖掘性质。

为了方便观察,我们先移项得到 fx,y=min⁡i=0ax−1{fx−1,i+[y<i]+[y<ax−ax−1+i]}f_{x,y}=\min\limits_{i=0}^{a_{x-1}}\{f_{x-1,i}+[y<i]+[y<a_x-a_{x-1}+i]\}。然后不难发现对于同一个决策 ii,yy 越大答案越小,且极差不超过 22,将所有决策的答案取 min⁡\min 后 fx,yf_{x,y} 显然仍然满足这两个性质,且对于 fx,yf_{x,y} 相同的一段,最优一定是取 yy 最小的那个。

所以我们只需要记录一个 ans,p,qans,p,q 分别表示 fx,yf_{x,y} 的最小值,以及 ans+1ans+1 和 ansans 最小的位置,形式化来说就是:

fx,y={ans−2y∈[0,p−1]ans−1y∈[p,q−1]ansy∈[q,ax]f_{x,y}= \begin{cases} ans-2&y\in [0,p-1]\\ ans-1&y\in [p,q-1]\\ ans&y\in [q,a_x]\\ \end{cases}

现在的转移方程就为(为了方便,这里令 Δ=ax−ax−1\Delta=a_x-a_{x-1}):

fx,y=min⁡{ans+2+[y<Δ]ans+1+[y<p]+[y<Δ+p]ans+[y<q]+[y<Δ+q]f_{x,y}=\min \begin{cases} ans+2+[y<\Delta]\\ ans+1+[y<p]+[y<\Delta+p]\\ ans+[y<q]+[y<\Delta+q] \end{cases}

然后考虑如何动态维护 ans,p,qans,p,q,分类讨论:

  • ax<qa_x<q,ansans 显然会加一,此时取不到 ans+2ans+2,因此 p=0p=0。再考虑 qq,对于第二种转移,yy 要满足 p≤yp\le y,对于第三种转移,因为 Δ<0\Delta<0,所以 yy 要满足 Δ+q≤y\Delta+q\le y,因此新的 qq 就为 min⁡(p,Δ+q)\min(p,\Delta+q),注意这里 Δ+q\Delta+q 有可能小于 00,因此 qq 还要和 00 取 max⁡\max。
  • q≤ax<ax−1q\le a_x<a_{x-1},此时 ansans 不变,因为 Δ<0\Delta<0,所以和上面同理,pp 就为 max⁡(min⁡(p,Δ+q),0)\max(\min(p,\Delta+q),0)。对于 qq,在第三种转移中需要满足 y≥qy\ge q,所以 qq 不变。
  • ax−1≤axa_{x-1}\le a_x,此时 ansans 不变,Δ≥0\Delta\ge 0。然后考虑 pp,对于第二种转移,要满足 Δ+p≤y\Delta+p\le y,对于第三种转移,要满足 q≤yq\le y,因此 p=min⁡(Δ+p,q)p=\min(\Delta+p,q)。对于 qq,在第三种转移中需要满足 y≥Δ+qy\ge \Delta+q,因此直接把 qq 加上 Δ\Delta 即可。

时间复杂度 O(n)O(n)。

Code

C++
//when you use vector or deque,pay attention to the size of it.
//by OldDirverTree
#include<bits/stdc++.h>
//#include<atcoder/all>
#define P pair<int,int>
#define int long long
#define mid (l+r>>1)
using namespace std;
//using namespace atcoder;
//using mint=modint998244353;
int n,ans,p,q;

struct custom_hash
{
    static uint64_t splitmix64(uint64_t x) {
        x+=0x9e3779b97f4a7c15;
        x=(x^(x>>30) )*0xbf58476d1ce4e5b9;
        x=(x^(x>>27) )*0x94d049bb133111eb;
        return x^(x>>31);
    }
    size_t operator() (uint64_t x) const {
        static const uint64_t FIXED_RANDOM=chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x+FIXED_RANDOM);
    }
};
int read() {
    int x=0; bool _=true; char c=0;
    while (!isdigit(c) ) _&=(c!='-'),c=getchar();
    while (isdigit(c) ) x=x*10+(c&15),c=getchar();
    return _?x:-x;
}
main()
{
    n=read();
    for (int i=1,lst=0;i<=n+1;i++) {
        int val=i<=n?read():0;
        if (val<q) ans++,q=max(min(p,val-lst+q),0ll),p=0;
        else if (val<lst) p=max(min(p,val-lst+q),0ll);
        else p=min(p+val-lst,q),q+=val-lst; lst=val;
    }
    printf("%lld",ans);
    return 0;
}
原文