题目链接
https://nuoyanli.com/contest/1/problem/2D
题面
题意
给定 个数, 次询问每次一个 问至少要到第几个数的和才大于等于 。
思路
因为是求和,所以前缀和处理一下,由于
和
都在
所以暴力找肯定超时(数据没给满导致很多人水过Orz),所以考虑二分,由于前缀和具有单调性,所以二分可行。
注意
标程
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6+7;
LL sum[N],a[N],n,Q,k;
int main() {
scanf("%lld%lld",&n,&Q);
for(int i=1; i<=n; i++) {
scanf("%lld",&a[i]);
sum[i]=sum[i-1]+a[i];
}
while(Q--) {
scanf("%lld",&k);
printf("%d\n",lower_bound(sum+1,sum+n+1,k)-sum);
}
return 0;
}