Codeforces 1249 E. By Elevator or Stairs?

Portal

First, obviously down operation is not necessarily superior, so as far as the stairs

Provided $ f [i] $ represents the minimum time required when the layer of $ I $

Consider then the first stairs, metastasis, $ f [i] = f [i-1] + a [i-1] $

Consider then take the elevator has: $ f [i] = f [j] + (\ sum_ {k = j} ^ {i-1} b [k]) + c $

Apparently that $ \ sum b $ prefix and can do it, then $ f [i] = f [j] + sum [i-1] -sum [j-1] + c $

We just transferred $ dp $ maintain a current $ f [j] -sum [j-1] $ minimum to $ mi $

即 $f[i]=mi+sum[i-1]+c$

Do not ask me why I want to write forcibly tree line, I smoked brain

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=2e5+7;
const ll INF=1e18;
ll n,m,A[N],B[N];
struct SegTree {
    ll t[N<<2];
    inline void ins(int o,int l,int r,int pos,int v)
    {
        if(l==r) { t[o]+=v; return; }
        int mid=l+r>>1;
        pos<=mid ? ins(o<<1,l,mid,pos,v) : ins(o<<1|1,mid+1,r,pos,v);
        t[o]=min(t[o<<1],t[o<<1|1]);
    }
    inline ll query(int o,int l,int r,int ql,int qr)
    {
        if(l>=ql&&r<=qr) return t[o];
        if(l>qr||r<ql) return INF;
        int mid=l+r>>1;
        return min(query(o<<1,l,mid,ql,qr),query(o<<1|1,mid+1,r,ql,qr));
    }
}T;
ll f[N];
int main()
{
    n=read(),m=read();
    for(int i=2;i<=n;i++) A[i]=read();
    for(int i=2;i<=n;i++) B[i]=B[i-1]+read();
    for(int i=2;i<=n;i++)
    {
        f[i]=f[i-1]+A[i];
        f[i]=min(f[i],T.query(1,1,n,1,i-1)+B[i]+m);
        T.ins(1,1,n,i,f[i]-B[i]);
    }
    for(int i=1;i<=n;i++) printf("%lld ",f[i]); puts("");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/LLTYYC/p/11756613.html