1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<climits> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; typedef long long LL; inline const LL Get_Int() { LL num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=100005; struct Tree { int left,right; LL min; Tree(int l=0,int r=0,LL m=LLONG_MAX/2):left(l),right(r),min(m) {} }; struct Segment_Tree { Tree tree[maxn*4]; #define ls index<<1 #define rs index<<1|1 void build(int index,int Left,int Right) { tree[index]=Tree(Left,Right); if(Left==Right)return; int mid=(Left+Right)>>1; build(ls,Left,mid); build(rs,mid+1,Right); } void push_up(int index) { tree[index].min=min(tree[ls].min,tree[rs].min); } void modify(int index,int target,LL v) { if(target>tree[index].right||target<tree[index].left)return; if(tree[index].left==tree[index].right) { tree[index].min=min(tree[index].min,v); return; } modify(ls,target,v); modify(rs,target,v); push_up(index); } LL query(int index,int Left,int Right) { if(Left>tree[index].right||Right<tree[index].left)return LLONG_MAX/2; if(Left<=tree[index].left&&Right>=tree[index].right)return tree[index].min; return min(query(ls,Left,Right),query(rs,Left,Right)); } } st[7]; int n; LL x,sum[maxn],sum2[maxn],ans=LLONG_MAX; pair<LL,LL> bound(int pos,int id) { LL up,down; if(id==1) { down=ceil((sum[n]+sum[pos])/2.0); up=2*sum[pos]; } if(id==2) { down=sum[n]-sum[pos]; up=(sum[n]+sum[pos])/2.0; } if(id==3) { down=ceil((sum[n]+sum[pos])/2.0); up=sum[n]-sum[pos]; } if(id==4) { down=max(2*sum[pos],sum[n]-sum[pos]); up=1e17; } if(id==5) { down=-1e17; up=min(2*sum[pos],sum[n]-sum[pos]); } if(id==6) { down=2*sum[pos]; up=(sum[n]+sum[pos])/2.0; } return make_pair(down,up); } LL segment_val(int pos,int id) { if(id==1)return sum[pos]+x*pos; if(id==2)return x*pos-sum[pos]; if(id==3)return sum[pos]+x*pos; if(id==4)return 2*sum[pos]+x*pos; if(id==5)return x*pos-2*sum[pos]; if(id==6)return x*pos-sum[pos]; } LL get_ans(int pos,int id) { if(id==1)return sum[pos]-sum[n]-x*pos; if(id==2)return 2*sum[pos]-x*pos; if(id==3)return -2*sum[pos]-x*pos; if(id==4)return -sum[pos]-x*pos-sum[n]; if(id==5)return sum[n]+sum[pos]-x*pos; if(id==6)return sum[n]-sum[pos]-x*pos; } void Solve(int pos,int id) { pair<LL,LL> tmp=bound(pos,id); LL down=tmp.first,up=tmp.second; int l=lower_bound(sum2+1,sum2+n+1,down)-sum2,r=upper_bound(sum2+1,sum2+n+1,up)-sum2-1; if(l>r)return; ans=min(ans,get_ans(pos,id)+st[id].query(1,l,r)); } struct number { LL s; int id; bool operator < (const number& b) const { return s<b.s; } } a[maxn]; int M[maxn]; int main() { n=Get_Int(); for(int i=1; i<=n; i++) { sum[i]=a[i].s=a[i-1].s+Get_Int(); a[i].id=i; } x=Get_Int(); sort(a+1,a+n+1); for(int i=1; i<=n; i++)M[a[i].id]=i,sum2[i]=a[i].s; for(int i=1; i<=6; i++)st[i].build(1,1,n); for(int i=n-2; i>=1; i--) for(int j=1; j<=6; j++) { st[j].modify(1,M[i+1],segment_val(i+1,j)); Solve(i,j); } printf("%lld\n",ans); return 0; }
|