题面
代码
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define mid (l+r)/2
#define ls i<<1
#define rs i<<1|1
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
#define N 100100
ll dp[N],s[N],t[N<<2];
int a[N],n;
ll sum(int l,int r){
if(l>r) return 0;
return s[r]-s[l-1];
}
int find(int ri,ll x){
int l=0,r=ri-1;
int ans=-1;
while(l<=r){
//cout<<l<<" "<<r<<endl;
//return -1;
if(sum(mid+1,ri-1)<=x) ans=mid,r=mid-1;
else l=mid+1;
}
return ans;
}
void pu(int i){
t[i]=min(t[ls],t[rs]);
}
void cha(int i,int l,int r,int pos,ll k){
if(l==r&&r==pos){
t[i]=k;return;
}
if(pos<=mid) cha(ls,l,mid,pos,k);
else cha(rs,mid+1,r,pos,k);
pu(i);
}
ll que(int i,int l,int r,int cl,int cr){
//cout<<l<<" "<<r<<endl;
if(l>=cl&&r<=cr) return t[i];
if(l>cr||r<cl) return 1e15;
return min(que(ls,l,mid,cl,cr),que(rs,mid+1,r,cl,cr));
}
bool check(ll x){
cha(1,0,n+1,0,0);
rep(i,1,n+1){
int idx=find(i,x);
//cout<<i<<" "<<x<<" "<<idx<<endl;
dp[i]=a[i]+que(1,0,n+1,idx,i-1);
//cout<<que(1,0,n+1,idx,i-1)<<" "<<dp[i]<<endl;
cha(1,0,n+1,i,dp[i]);
}
if(dp[n+1]>x) return false;
else return true;
}
void solve(){
rep(i,1,n+1) a[i]=0;
rep(i,1,4*n+100) t[i]=1e15;
cin>>n;
rep(i,1,n) cin>>a[i],s[i]=s[i-1]+a[i];
ll l=1,r=1e14+10;
ll ans=-1;
while(l<=r){
//cout<<l<<" "<<r<<endl;
if(check(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
cout<<ans<<endl;
}
int main(){
IOS
int t=1;cin>>t;
while(t--) solve();
return 0;
}
/*
3
6
1 4 5 3 3 2
5
1 2 3 4 5
6
4 1 6 3 10 7
1
6
1 4 5 3 3 2
*/