D. Xenia and Colorful Gems

总共有三组数,要求 (xy+(yz+(zx最小值,

共有如下几种情况

  • a<=b<=c
  • a<=c<=
  • b<=a<=
  • b<=c<=
  • c<=a<=
  • c<=b<=

我们考虑枚举中间的数,然后让两边的数趋近于它,这样最后得到的结果就一定满足条件。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=1e5+10;
 5 inline ll calc(ll x,ll y,ll z){return (x - y) * (x - y) + (y - z) * (y - z) + (z - x) * (z - x);}
 6 int n[3],v[3][maxn];
 7 int main(){
 8     int t;
 9     cin>>t;
10     while(t--){
11         cin>>n[0]>>n[1]>>n[2];
12         for(int i=0;i<3;i++){
13             for(int j=0;j<n[i];j++){
14                 cin>>v[i][j];
15             }
16             sort(v[i],v[i]+n[i]);
17         }
18         ll ans=6e18+10;
19         for(int p1=0;p1<3;p1++){
20             for(int p2=0;p2<3;p2++){
21                 for(int p3=0;p3<3;p3++){
22                     if(p1==p2||p2==p3||p3==p1)continue;
23                     for(int i=0;i<n[p1];i++){   //中间数
24                         int pos1=lower_bound(v[p2],v[p2]+n[p2],v[p1][i])-v[p2];  //右边数
25                         int pos2=upper_bound(v[p3],v[p3]+n[p3],v[p1][i])-v[p3];  //左边数
26                         if(pos1!=n[p2]&&pos2!=0){  //假如不存在右边数或者不存在左边数
27                             ans=min(ans,calc(v[p1][i],v[p2][pos1],v[p3][pos2-1]));
28                         }
29                     }
30                 }
31             }
32         }
33         cout<<ans<<"\n";
34     }
35     return 0;
36 }
View Code

 

posted @ 2020-04-20 21:38  古比  阅读(161)  评论(0编辑  收藏  举报