跳到正文

剪贴板

剪贴板 cjfqv2pp

原文
剪贴板信息

剪贴板信息

收录
更新

代码专用Paste。

C++
#include<bits/stdc++.h>

#define rep(x,y,z) for(int x=y;x<=z;x++)
#define per(x,y,z) for(int x=z;x>=y;x--)
#define LL long long

using namespace std;

void work()
{
	int n,q;
	cin>>n>>q;
	vector<int>a(n);
	rep(i,0,n-1) cin>>a[i];
	sort(a.begin(),a.end());
	vector<int>mi(n);
	vector<LL>s(n);
	mi[0]=a[0];
	s[0]=a[0];
	rep(i,1,n-1) mi[i]=min(mi[i-1],a[i]-i),s[i]=s[i-1]+a[i]-i;
	while(q--)
	{
		int k;
		cin>>k;
		if(n==1)
		{
			if(k&1) cout<<a[0]+k-k/2<<' ';
			else cout<<a[0]-k/2<<' ';
			continue;
		}
		auto pd=[&](LL x)
		{
			int c=lower_bound(a.begin(),a.end(),x)-a.begin();
			if(c==0) return 1;
			if(c>k) return 0;
			if(mi[c-1]+k<x) return 0;
			if(n==c&&(k-c)%2) return 0;
			LL res=k-c-2*(s[c-1]+1ll*c*(k-x));
			if(res<=0) return 1;
			if(n==c) return 0;
			if(res%2||n-c>=2) return 1;
			int flag=a[n-1]-res/2>=x;
			return flag;
		};
		LL l=1,r=1e10,ans;
		while(l<=r)
		{
			LL mid=l+r>>1;
			if(pd(mid)) ans=mid,l=mid+1;
			else r=mid-1;
		}
		cout<<ans<<' ';
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	work();
	return 0;
}