跳到正文

个人记录

1

00001A约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月16日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,q,a[N],k;
bool ok(int x){
	int l=0,r=n+1;
	while(l<r-1){
		int m=(r+l)>>1;
		if(a[m]<x)l=m;
		else r=m;
	}
	//cout<<x<<' '<<l<<"\n";
	if(l==0){
		if(n==1&&!k&1)return a[1]-k>>1>=x;
		return 1;
	}
	if(k<l)return 0;
	int s=0;
	for(int i=1;i<=l;i++){
		if(a[i]+k-i+1<x)return 0;
		s+=a[i]+k-i+1-x;
	}
	int e=k-l-s*2;
	cout<<x<<" "<<e<<"\n";
	if(n==l)return 0;
	if(n-l>=2)return 1;
	if(e&1)return 1;
	if(a[n]-(e>>1)>=x)return 1;
	return 0;
}
signed main()
{
	cin>>n>>q;
	for(int i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+1+n);
	while(q--){
		cin>>k;
		int l=-1e9,r=1e9;
		while(l<r){
			int m=(l+r+1)>>1;
			if(ok(m))l=m;
			else r=m-1;
		}
		cout<<l<<endl;
	}
}
原文