AT_agc044_e [AGC044E] Random Pawn 题解

· · 题解

:::::success[Hint 1] 列出转移方程式,观察性质后断环为链。 ::::: :::::success[Hint 2] 考虑去除 -b_i 项,考虑列出另一个形式和它较像的式子。 ::::: :::::success[Hint 3] 考虑观察凸性,并求出答案。 :::::

对应 Hint 1:

我们设 f_i 表示位于 i 号位置的期望,容易得到转移:

f_i=\max(a_i,\frac{f_{i-1}+f_{i+1}}{2}-b_i)

转移是具有后效性的,不过我们先考虑能否断环为链。

钦定 a_n 为整个序列的最大值,那么我们注意到 f_n=a_n 一定成立,那么我们考虑从这里断环,我们令 a_0=a_n,b_0=b_n,这样就变成在 [0,n] 的序列上做了。

对应 Hint 2:

我们发现这个 -b_i 比较烦人,我们考虑去掉 -b_i 项,我们考虑构造另一个和 f_i 形式很像的序列相减来抵消,我们设 g_i 满足:

g_i=\frac{g_{i-1}+g_{i+1}}{2}-b_i

变形得到:

g_{i+1}=2(g_i-b_i)-g_{i-1}

那么我们考虑能否真的抵消,我们考虑 F_i=f_i-g_i,我们能得到:

\begin{aligned}F_i&=\max(a_i-g_i,\frac{F_{i-1}+g_{i-1}+F_{i+1}+g_{i+1}}{2}-b_i-g_i)\\&=\max(a_i-g_i,\frac{F_{i-1}+g_{i-1}+F_{i+1}+2(g_i-b_i)-g_{i-1}}{2}-b_i-g_i)\\&=\max(a_i-g_i,\frac{F_{i-1}+F_{i+1}}{2})\end{aligned}

对应 Hint 3:

由于 F_i\ge\frac{F_{i-1}+F_{i+1}}{2} 成立,所以 F_i 构成一个上凸包,而且还是所有 (i,a_i-g_i) 构成的上凸包。

那么我们要求 \frac 1n\sum_{i=1}^nf_i,也就是说我们要求 \sum_{i=1}^nF_i,我们直接对每个梯形面积求出再求和即可。

这样我们就做完了这道题。
时空复杂度均为 \Theta(n)。

:::::success[code]

#include<bits/stdc++.h>
#define inl inline
#define int long long
#define lll __int128
#define fst ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,x,y) for(int i=x;i<=(y);++i) 
#define per(i,x,y) for(int i=x;i>=(y);--i)
#define rpr(i,x,y,z) for(int i=x;i<=(y);i+=z)
#define epe(i,x,y,z) for(int i=x;i>=(y);i-=z)
#define repe(i,x,y) for(i=x;i<=(y);++i) 
#define endl '\n'
#define INF 1e18
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front 
#define fi first
#define se second
#define lcm(x,y) x/__gcd(x,y)*y
#define ull unsigned long long
#define prr make_pair
#define pii pair<int,int> 
#define gt(s) getline(cin,s)
#define at(x,y) for(const auto &x:y)
#define ff fflush(stdout)
#define mt(x,y) memset(x,y,sizeof(x))
#define idg isdigit
#define fp(s) string ssss=s;freopen((ssss+".in").c_str(),"r",stdin);freopen((ssss+\
".out").c_str(),"w",stdout);
#define sstr stringstream 
#define all(x) x.begin(),x.end()
#define mcy(a,b) memcpy(a,b,sizeof(b))
#define ui unsigned
#define si signed
#define eb emplace_back
#define pff(x) ((x)*(x))
#define eush emplace
#define double long double
#define pdi pair<double,int>
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
#ifdef __unix__
#define pc putchar_unlocked
#else
#define pc _putchar_nolock
#endif
using namespace std;
const int N=2e5+5;
int a[N],b[N],c[N],f[N];
struct node{
    int x,y;
    inl node(){
        x=y=0;
    }
    inl node(int x,int y):x(x),y(y){}
};
stack<node>st;
inl node top2(){
    node x=st.top();
    st.pop();
    node y=st.top();
    st.push(x);
    return y;
}
signed main(){
    fst;
    int n,id=0,cnt=0,ans=0;
    cin>>n;
    rep(i,1,n) cin>>a[i];
    rep(i,1,n) cin>>b[i];
    rep(i,1,n)
        if(a[i]>=a[id]) id=i;
    rep(i,id+1,n) c[++cnt]=a[i];
    rep(i,1,id) c[++cnt]=a[i];
    rep(i,1,n) a[i]=c[i];
    cnt=0;
    rep(i,id+1,n) c[++cnt]=b[i];
    rep(i,1,id) c[++cnt]=b[i];
    rep(i,1,n) b[i]=c[i];
    a[0]=a[n];
    b[0]=b[n];
    f[0]=f[1]=0;
    rep(i,2,n) f[i]=(f[i-1]+b[i-1]<<1)-f[i-2];
//  rep(i,0,n) cout<<f[i]<<' ';
//  cout<<endl; 
    rep(i,0,n){
        while(st.size()>=2&&(a[i]-f[i]-top2().y)*(st.top().x-top2().x)>=(st.top().y-top2().y)*(i-top2().x)) st.pop();
        st.eush(i,a[i]-f[i]);
    }
    while(st.size()>=2){ 
        ans+=(st.top().y+top2().y)*(st.top().x-top2().x+1)-(top2().y<<1);
//      cout<<st.top().x<<' '<<st.top().y<<' '<<top2().x<<' '<<top2().y<<' '<<ans<<endl;
        st.pop();
    }
    rep(i,2,n) ans+=f[i]<<1;
    cout<<fixed<<setprecision(12)<<ans/2.0/n<<endl;
    return 'T'^'0'^'m'^'l'^'e';
}

:::::