链接
题目描述
思路
可以发现只需要a的总和为(B-1)的倍数就好了
那么直接用前缀和然后二分这个位置就好了
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
ll B, q, a[1000006];
ll h[1000006];
ll sum = 0;
ll ask(ll x)
{
ll pl = lower_bound(h, h + B, x) - h;
if(pl == B) return -1;
return pl;
}
int main()
{
scanf("%lld%lld", &B, &q);
for(int i = 1; i <= B; ++i)
{
scanf("%lld", &a[i - 1]);
sum += (i - 1) * a[i - 1];
}
sum %= (B - 1);
ll now = sum;
while(sum > 0)
{
if(a[now]) sum -= now, a[now]--;
else now--;
if(sum == 0) break;
}
for(int i = 0; i <= B - 1; ++i)
h[i] = h[max(i - 1, 0)] + a[i];
for(int i = 1; i <= q; ++i)
{
ll x;
scanf("%lld", &x);
ll ans = ask(++x);
printf("%lld\n", ans);
}
return 0;
}