思路
首先我们可以定义一个 数组,表示前 个数字中前缀和的最大值,以便于后续二分。若第一轮搞不定,而且一轮的总和还是非正,说明永远不会再长进了,无解了。否则,就类似蜗牛爬井,可以计算它需要在第几轮里成功满足条件,然后二分一下即可。
代码
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int t, n, m;
ll a[200005], sum, x, s;
int main () {
ios::sync_with_stdio (0);
cin.tie (0);
cout.tie (0);
cin >> t;
while (t --) {
cin >> n >> m;
sum = 0;
for (int i = 1; i <= n; ++ i)
cin >> a[i], a[i] = max (a[i - 1], sum += a[i]);
while (m --) {
cin >> x;
if (x <= a[n])
s = 0;
else if (sum > 0)
s = (x - a[n] + sum - 1) / sum;
else {
cout << "-1 ";
continue ;
}
x -= s * sum;
cout << lower_bound (a + 1, a + 1 + n, x) - a - 1 + s * n << ' ';
}
cout << '\n';
}
return 0;
}