1 solutions
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 神兽保佑 永无BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define speMOD 2933256077ll #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<(x)<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define rf(v) shuffle(all(v),sd); #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); int qpow(int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } string YN(bool x,string Y="Yes",string N="No") { if(x){ return Y; } else { return N; } } int exgcd(int x,int y,int &a,int &b){ if(y==0){ a=1;b=0; return x; } int d=exgcd(y,x%y,a,b); int z=b; b=a-b*(x/y); a=z; return d; } int _x_,_y_; inline int inverse(int x,int y=MOD){ exgcd(x,y,_x_,_y_); return (_x_+y)%y; } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } int n; int a[500005]; int dp[500005]; pii st[500005][20]; int qmin(int l,int r){ int s=__lg(r-l+1); return -min(st[l][s],st[r-(1<<s)+1][s]).second; } int ps[500005]; int L[500005],R[500005]; int query(int l,int r){ int m=qmin(l,r); if(a[m]==a[r]){ if(R[l]==r)return ps[r]-ps[l+1]-a[l]*(r-l-2)-a[r]+1; return (ps[r+1]-ps[l+1])-a[l]*(R[l]-l-1)-a[r]*(r-R[l]+1)+1; } if(a[m]==a[l]){ if(L[r]==l)return ps[r]-ps[l+1]-a[r]*(r-l-2)-a[l]+1; return ps[r+1]-ps[l+1]-a[l]*(L[r]-l)-a[r]*(r-L[r])+1; } return ps[r+1]-ps[l+1]-a[l]*(R[l]-l-1)-a[r]*(r-L[r])-(L[r]-R[l]+1)*a[qmin(R[l],L[r])]; } void Main() { cin>>n; REP(i,0,n)cin>>a[i],st[i][0]={a[i],-i}; REP(j,0,__lg(n)){ REP(i,0,n-(1<<(j+1))+1)st[i][j+1]=min(st[i][j],st[i+(1<<j)][j]); } REP(i,0,n)ps[i+1]=ps[i]+a[i]; stack<int>st; REP(i,0,n){ while(!st.empty()&&a[st.top()]>=a[i])st.pop(); L[i]=(st.empty()? -1:st.top());st.push(i); } while(!st.empty())st.pop(); for(int i=n-1;i>=0;--i){ while(!st.empty()&&a[st.top()]>=a[i])st.pop(); R[i]=(st.empty()? n:st.top());st.push(i); } REP(i,0,n){ if(L[i]==-1)dp[i]=ps[i]-i*a[i]; else dp[i]=ps[i]-(i-L[i]-1)*a[i]-a[qmin(0,L[i])]*(L[i]+1); if(n<=100000)REP(j,i-min(1000ll,i),i-1)dp[i]=min(dp[i],dp[j]+query(j,i)); else REP(j,i-min(400ll,i),i-1)dp[i]=min(dp[i],dp[j]+query(j,i)); } int ans=1e18; REP(i,0,n){ if(R[i]==n)ans=min(ans,dp[i]+(ps[n]-ps[i+1])-a[i]*(n-i-1)); else ans=min(ans,dp[i]+ps[n]-ps[i+1]-a[i]*(R[i]-i-1)-a[qmin(R[i],n-1)]*(n-R[i])); } over(ans) } void TC() { int tc=1; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("sequence.in","r",stdin); freopen("sequence.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 2934
- Time
- 4000ms
- Memory
- 512MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 2
- Accepted
- 2
- Uploaded By