CF1336 B. Xenia and Colorful Gems

9 篇文章 0 订阅
3 篇文章 0 订阅

B. Xenia and Colorful Gems

题意

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

思路

枚举+二分
数学上来说固定三个数中最大的一个数,再固定一个最小的数,第三个数在两个数正中间时,才是最小的,稍微偏离一点就会增大。
固定一个数组中的某个元素 u u u,然后选一个大于等于 u u u x x x,最后选一个最接近 u + x 2 \frac{u+x}{2} 2u+x的数。
第三个数的位置二分出来可以为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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值