Codeforces 685C - Optimal Point(分类讨论+乱搞)

Codeforces 题面传送门 & 洛谷题面传送门

分类讨论神题。

首先看到最大值最小,一眼二分答案,于是问题转化为判定性问题,即是否 \(\exists x_0,y_0,z_0\) 满足 \(\forall i,|x_0-x_i|+|y_0-y_i|+|z_0-z_i|\le mid\)。

柿子中带个绝对值,不好直接转化。不过注意到对于任意实数 \(x\) 都有 \(x\le |x|,-x\le |x|\),因此 \(|x-y|\le v\) 的充要条件即是 \(x-y\le v,y-x\le v\),因此上式可以把绝对值拆开得到:

\[\begin{cases} x_0-x_i+y_0-y_i+z_0-z_i\le mid\\ x_0-x_i+y_0-y_i+z_i-z_0\le mid\\ x_0-x_i+y_i-y_0+z_0-z_i\le mid\\ x_0-x_i+y_i-y_0+z_i-z_0\le mid\\ x_i-x_0+y_0-y_i+z_0-z_i\le mid\\ x_i-x_0+y_0-y_i+z_i-z_0\le mid\\ x_i-x_0+y_i-y_0+z_0-z_i\le mid\\ x_i-x_0+y_i-y_0+z_i-z_0\le mid \end{cases} \]

将上式整理一下可以得到:

\[\begin{cases} x_i+y_i+z_i-mid\le x_0+y_0+z_0\le x_i+y_i+z_i+mid\\ -x_i+y_i+z_i-mid\le -x_0+y_0+z_0\le -x_i+y_i+z_i+mid\\ x_i-y_i+z_i-mid\le x_0-y_0+z_0\le x_i-y_i+z_i+mid\\ x_i+y_i-z_i-mid\le x_0+y_0-z_0\le x_i+y_i-z_i+mid \end{cases} \]

记 \(A_{l}=\max\limits_{i=1}^nx_i+y_i+z_i-mid,A_{r}=\min\limits_{i=1}^nx_i+y_i+z_i-mid\),\(B_{l},B_r,C_l,C_r,D_l,D_r\) 也同理,那么显然上述柿子可以等效于:

\[\begin{cases} A_l\le x_0+y_0+z_0\le A_r\\ B_l\le -x_0+y_0+z_0\le B_r\\ C_l\le x_0-y_0+z_0\le C_r\\ D_l\le x_0+y_0+z_0\le D_r \end{cases} \]

注意到这四个不等式中间一项都带三个未知数,不好处理,不过这三个未知数之间又存在某种联系,因此考虑记 \(a=-x_0+y_0+z_0,b=x_0-y_0+z_0,c=x_0+y_0-z_0\),那么有 \(x_0=\dfrac{b+c}{2},y_0=\dfrac{a+c}{2},z_0=\dfrac{a+b}{2}\)。我们还可以注意到 \(x_0+y_0+z_0=a+b+c\),因此柿子又转化为

\[\begin{cases} A_l\le a+b+c\le A_r\\ B_l\le a\le B_r\\ C_l\le b\le C_r\\ D_l\le c\le D_r \end{cases} \]

当然这里有一个 restriction 是 \(x_0,y_0,z_0\) 必须都是整数,因此必须有 \(a\equiv b\equiv c\pmod{2}\)。故考虑枚举 \(r=a\bmod 2\),记 \(a'=\dfrac{a-r}{2},b'=\dfrac{b-r}{2},c'=\dfrac{c-r}{2}\),那么上式又变为:

\[\begin{cases} \lceil\dfrac{A_l-3r}{2}\rceil\le a'+b'+c'\le\lfloor\dfrac{A_r-3r}{2}\rfloor\\ \lceil\dfrac{B_l-r}{2}\rceil\le a'\le\lfloor\dfrac{B_r-r}{2}\rfloor\\ \lceil\dfrac{C_l-r}{2}\rceil\le b'\le\lfloor\dfrac{C_r-r}{2}\rfloor\\ \lceil\dfrac{D_l-r}{2}\rceil\le c'\le\lfloor\dfrac{D_r-r}{2}\rfloor \end{cases} \]

这个随便求一求就行了,我相信即便刚学过 OI 的应该也会罢

时间复杂度 \(n\log A\),其中 \(A=\max\{a_i\}\)

const int MAXN=1e5;
const ll INF=7e18; 
int n;ll x[MAXN+5],y[MAXN+5],z[MAXN+5],X,Y,Z;
ll down(ll x){return (x>=0)?(x>>1):(-(-x+1>>1));}//\lfloor x/2 \rfloor
ll up(ll x){return (x>=0)?(x+1>>1):(-(-x>>1));}//\lceil x/2 \rceil
bool check(ll mid){
	ll al=-INF,ar=INF,bl=-INF,br=INF;
	ll cl=-INF,cr=INF,dl=-INF,dr=INF;
	for(int i=1;i<=n;i++){
		chkmax(al,x[i]+y[i]+z[i]-mid);chkmin(ar,x[i]+y[i]+z[i]+mid);
		chkmax(bl,-x[i]+y[i]+z[i]-mid);chkmin(br,-x[i]+y[i]+z[i]+mid);
		chkmax(cl,x[i]-y[i]+z[i]-mid);chkmin(cr,x[i]-y[i]+z[i]+mid);
		chkmax(dl,x[i]+y[i]-z[i]-mid);chkmin(dr,x[i]+y[i]-z[i]+mid);
	}
	for(int r=0;r<2;r++){
		ll wl=up(al-3*r),wr=down(ar-3*r);
		ll xl=up(bl-r),xr=down(br-r);
		ll yl=up(cl-r),yr=down(cr-r);
		ll zl=up(dl-r),zr=down(dr-r);
		if(wl<=wr&&xl<=xr&&yl<=yr&&zl<=zr&&xl+yl+zl<=wr&&xr+yr+zr>=wl){
			ll a=xl,b=yl,c=zl,need=max(0ll,wl-xl-yl-zl);
			a+=min(need,xr-xl);need-=min(need,xr-xl);
			b+=min(need,yr-yl);need-=min(need,yr-yl);
			c+=min(need,zr-zl);need-=min(need,zr-zl);
			a=(a<<1|r);b=(b<<1|r);c=(c<<1|r);
			X=b+c>>1;Y=c+a>>1;Z=a+b>>1;return 1;
		}
	} return 0;
}
void solve(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%lld%lld%lld",&x[i],&y[i],&z[i]);
	ll l=0,r=INF>>1,p=-1;
	while(l<=r){
		ll mid=l+r>>1;
		if(check(mid)) p=mid,r=mid-1;
		else l=mid+1;
	} check(p);//printf("%lld\n",p);
	printf("%lld %lld %lld\n",X,Y,Z);
}
int main(){
	int qu;scanf("%d",&qu);
	while(qu--) solve();
	return 0;
}
上一篇:一道高中不等式题


下一篇:比较大小