题意
给定 n n n 个水槽的容量,每个水槽装满水后,多余的水将向下一个水槽转移(转移不耗费时间),每个水槽仅对接一个注水管,开启后每秒可以注入 1 1 1 单位的水。问是否能在 t t t 秒内将所有水槽都装满,以及能够装满的情况下需要打开注水管的最少数目。
容易想到在确定能够装满的情况下,最少数目即 水 槽 总 容 量 时 间 \frac{水槽总容量}{时间} 时间水槽总容量 上取整。由于水的转移具有前效性,考虑用前缀和的思想预处理前 i i i 个水槽被装满所需要的时间,由于每个水槽仅对接一个注水管,维护 m a x x = m a x ( m a x x , 前 i 个 水 槽 总 容 量 i ) maxx = max(maxx,\frac{前 i 个水槽总容量}{i}) maxx=max(maxx,i前i个水槽总容量) ,则在询问中所有小于 m a x x maxx maxx 的时间都为 − 1 -1 −1。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
int a[N],sum[N];
void solve(){
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
int q,x;
cin>>q;
int maxx=0;
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]+a[i];
int temp=sum[i]/i;
if(sum[i]%i)
temp++;
maxx=max(maxx,temp);
}
while(q--){
cin>>x;
if(x<maxx)
cout<<"-1"<<endl;
else{
int ans=sum[n]/x;
if(sum[n]%x)
ans++;
cout<<ans<<endl;
}
}
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0);
int T=1;
// cin>>T;
while(T--)
solve();
return 0;
}