Codeforces675 E. Trains and Statistic(dp,线段树)

Posted live4m

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces675 E. Trains and Statistic(dp,线段树)相关的知识,希望对你有一定的参考价值。

题意:

解法:

设d[i]表示点i到[i+1,n]的总代价
转移:
考虑下一步走到k,
画个图算一下贡献:

化简一下变为:
d[i]=(d[k]+k)+(n-a[i]-i).
发现只需要用线段树维护(d[k]+k)的最小值即可.

算法总复杂度O(n*log).

ps:
可以直接找范围内最大的a[k]对应的k,
因为a[k]最大能一步走的范围最大,总代价最小.

code:

#include<bits/stdc++.h>
// #define SYNC_OFF
#define int long long
#define ll long long
#define ull unsigned long long
//fast-coding
#define ST(x) x.begin()
#define ED(x) x.end()
#define RST(x) x.rbegin()
#define RED(x) x.end()
#define CL(x) x.clear();
#define all(a,n) a+1,a+1+n
#define ff(i,n) for(ll i=1;i<=n;i++)
#define rff(i,n) for(ll i=n;i>=1;i--)
#define fff(i,n) for(ll i=0;i<n;i++)
#define rfff(i,n) for(ll i=n-1;i>=0;i--)
#define SC(x) scanf("%s",x)
#define SL(x) strlen(x)
#define pss(a) push_back(a)
#define ps(a) push(a)
#define SZ(x) (int)x.size()
#define pee puts("");
#define eee putchar(' ');
#define re readdd()
#define pr(a) printtt(a)
int readdd(){int x=0,f=1;char c=getchar();//
while(!isdigit(c)&&c!='-')c=getchar();
if(c=='-')f=-1,c=getchar();
while(isdigit(c))x=x*10+c-'0',c=getchar();
return f*x;}
void printtt(int x){if(x<0)putchar('-'),x=-x;//
if(x>=10)printtt(x/10);putchar(x%10+'0');}
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}//
int ppow(int a,int b,int mod){a%=mod;//
int ans=1%mod;while(b){if(b&1)ans=(long long)ans*a%mod;
a=(long long)a*a%mod;b>>=1;}return ans;}
bool addd(int a,int b){return a>b;}
int lowbit(int x){return x&-x;}
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
bool isdigit(char c){return c>='0'&&c<='9';}
bool Isprime(int x){
    for(int i=2;i*i<=x;i++)if(x%i==0)return 0;
    return 1;
}
void ac(int x){if(x)puts("YES");else puts("NO");}
//short_type 
#define VE vector<int>
#define PI pair<int,int>
//
using namespace std;
// const int mod=998244353;
const int mod=1e9+7;
const int maxm=2e6+5;
int a[maxm];
int n;
struct Tree{
    int a[maxm<<2];
    void pp(int node){
        a[node]=min(a[node*2],a[node*2+1]);
    }
    void upd(int x,int val,int l,int r,int node){
        if(l==r){
            a[node]=val;return ;
        }
        int mid=(l+r)/2;
        if(x<=mid)upd(x,val,l,mid,node*2);
        else upd(x,val,mid+1,r,node*2+1);
        pp(node);
    }
    int ask(int st,int ed,int l,int r,int node){
        if(st<=l&&ed>=r)return a[node];
        int mid=(l+r)/2;
        int ans=1e18;
        if(st<=mid)ans=min(ans,ask(st,ed,l,mid,node*2));
        if(ed>mid)ans=min(ans,ask(st,ed,mid+1,r,node*2+1));
        return ans;
    }
}T;
void solve(){
    n=re;
    ff(i,n-1)a[i]=re;
    int ans=0;
    T.upd(n,0+n,1,n,1);//d[n]=0
    for(int i=n-1;i>=1;i--){
        int d=T.ask(i+1,a[i],1,n,1);
        d+=(n-a[i]-i);
        ans+=d;
        T.upd(i,d+i,1,n,1);
    }
    pr(ans);pee;
}
void Main(){
    // #define MULTI_CASE
    #ifdef MULTI_CASE
    int T;cin>>T;while(T--)
    #endif
    solve();
}
void Init(){
    #ifdef SYNC_OFF
    ios::sync_with_stdio(0);cin.tie(0);
    #endif
    #ifndef ONLINE_JUDGE
    freopen("../in.txt","r",stdin);
    freopen("../out.txt","w",stdout);
    #endif
}
signed main(){
    Init();
    Main();
    return 0;
}

以上是关于Codeforces675 E. Trains and Statistic(dp,线段树)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces675E Trains and Statistic RMQ优化 dp+贪心

CodeForces 675E Trains and Statistic

codeforces 675C Money Transfers 贪心

codeforces 675D Tree Construction set

[CodeForces586D]Phillip and Trains

codeforces675D