【结论】【二分】荒地群猎

链接

荒地群猎

题目描述

在这里插入图片描述

思路

可以发现只需要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;
} 

猜你喜欢

转载自blog.csdn.net/LTH060226/article/details/119810767
今日推荐