CF1336 B. Xenia and Colorful Gems

B. Xenia and Colorful Gems

题意

给你三个数组长度分别为 r , g , b ( 1 r , g , b 1 e 5 ) r,g,b(1\leq r,g,b\leq1e5) ,从中分别选出一个数 x , y , y x,y,y ,问 ( x y ) 2 + ( y z ) 2 + ( z x ) 2 (x-y)^2+(y-z)^2+(z-x)^2 最小为多少?

思路

枚举+二分
数学上来说固定三个数中最大的一个数,再固定一个最小的数,第三个数在两个数正中间时,才是最小的,稍微偏离一点就会增大。
固定一个数组中的某个元素 u u ,然后选一个大于等于 u u x x ,最后选一个最接近 u + x 2 \frac{u+x}{2} 的数。
第三个数的位置二分出来可以为y或y-1的位置。
eg:在这里插入图片描述
第一个数固定了3,第二个数固定了6,如果第三个数按比(3+6+1)/2=5去二分会找到7,然而选4才是最优解,因为(3+6)/2=4.5,我们上取整令其为5了,那么还要下取整的结果就是前面一位。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e6 + 5;

ll r,g,b;
vector<int>p[3];

ll calc(ll a,ll b,ll c){
	return (a-b)*(a-b)+(a-c)*(a-c)+(b-c)*(b-c);
}

void solve(){
	cin>>r>>g>>b;
	for(int i=0;i<3;i++) p[i].clear();
	for(int i=0;i<r;i++) {int x;cin>>x;p[0].push_back(x);}
	for(int i=0;i<g;i++) {int x;cin>>x;p[1].push_back(x);}
	for(int i=0;i<b;i++) {int x;cin>>x;p[2].push_back(x);}
	for(int i=0;i<3;i++) sort(p[i].begin(),p[i].end());
	ll ans=1ll<<62;
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			for(int k=0;k<3;k++){
				if(i!=j&&i!=k&&j!=k){
					for(auto u:p[i]){
						auto x=lower_bound(p[j].begin(),p[j].end(),u);
						if(x==p[j].end()) continue;
						auto y=lower_bound(p[k].begin(),p[k].end(),(u+*x+1)/2);
						if(y!=p[k].end()) ans=min(ans,calc(u,*x,*y));
						if(y!=p[k].begin()) --y,ans=min(ans,calc(u,*x,*y));
					}
				}
			}
		}
	}
	cout<<ans<<'\n';
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T;cin>>T;
	while(T--){
		solve();
	}
	return 0;
}
发布了89 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Meulsama/article/details/105559748