BZOJ3830 : [Poi2014]Freight

首先为了保证发车时间都不同,T[i]=max(T[i],T[i-1]+1)

然后设f[i]表示前i列车回来的最早时刻

f[i]=min(max(T[i],f[j]+i-j-1)+s*2+i-j-1)

=min(max(-j+T[i],f[j]-2*j+i-1))+s*2+i-1

=min(

min(-j)+T[i],f[j]-j<=T[i]-i+1

min(f[j]-2*j)+i-1,f[j]-j>=T[i]-i+1

)+s*2+i-1

用Treap维护区间最小值即可

 

#include<cstdio>
typedef long long ll;
int n,s,i,t[1000010],seed;ll f,inf=1LL<<60;
inline void read(int&a){char ch;while(!(((ch=getchar())>='0')&&(ch<='9')));a=ch-'0';while(((ch=getchar())>='0')&&(ch<='9'))a*=10,a+=ch-'0';}
inline void max(int&x,int y){if(x<y)x=y;}
inline void up(ll&x,ll y){if(x>y)x=y;}
inline ll min(ll x,ll y){return x<y?x:y;}
inline int rand(){return seed=seed*233+17;}
struct node{
  int p;ll val,v1,minv1,v2,minv2;node*l,*r;
  node(){val=p=0,v1=minv1=v2=minv2=inf;l=r=NULL;}
  inline void up(){minv1=min(v1,min(l->minv1,r->minv1));minv2=min(v2,min(l->minv2,r->minv2));}
}*blank=new(node),*T;
inline void Rotatel(node*&x){node*y=x->r;x->r=y->l;x->up();y->l=x;y->up();x=y;}
inline void Rotater(node*&x){node*y=x->l;x->l=y->r;x->up();y->r=x;y->up();x=y;}
void Ins(node*&x,ll p,ll v1,ll v2){
  if(x==blank){
    x=new(node);x->val=p;x->l=x->r=blank;x->v1=x->minv1=v1;x->v2=x->minv2=v2;x->p=rand();
    return;
  }
  up(x->minv1,v1);up(x->minv2,v2);
  if(p==x->val){up(x->v1,v1);up(x->v2,v2);return;}
  if(p<x->val){
    Ins(x->l,p,v1,v2);
    if(x->l->p>x->p)Rotater(x);
  }else{
    Ins(x->r,p,v1,v2);
    if(x->r->p>x->p)Rotatel(x);
  }
}
ll Ask1(node*&x,ll p){
  if(x==blank)return inf;
  if(p==x->val)return min(x->v1,x->l->minv1);
  if(p>x->val)return min(x->v1,min(x->l->minv1,Ask1(x->r,p)));
  return Ask1(x->l,p);
}
ll Ask2(node*&x,ll p){
  if(x==blank)return inf;
  if(p==x->val)return min(x->v2,x->r->minv2);
  if(p<x->val)return min(x->v2,min(x->r->minv2,Ask2(x->l,p)));
  return Ask2(x->r,p);
}
int main(){
  blank->l=blank->r=blank;
  read(n),read(s);s=s*2-1;
  for(t[0]=-1,i=1;i<=n;i++)read(t[i]),max(t[i],t[i-1]+1);
  for(Ins(T=blank,0,0,0),i=1;i<=n;i++)f=min(Ask1(T,t[i]-i+1)+t[i],Ask2(T,t[i]-i+1)+i-1)+s+i,Ins(T,f-i,-i,f-i*2);
  return printf("%lld",f),0;
}

  

 

posted @ 2015-01-08 13:14  Claris  阅读(526)  评论(0编辑  收藏  举报