zoukankan      html  css  js  c++  java
  • Codeforces Round #635 (Div. 1)B(二分查找前驱后继)

    枚举第一个数x,根据第一个数找寻和它接近的第二个数y,再找寻和(x+y)/2接近的第三个数z。

     1 #define HAVE_STRUCT_TIMESPEC
     2 #include<bits/stdc++.h>
     3 using namespace std;
     4 long long r[100007],g[100007],b[100007];
     5 long long func(long long x,long long y,long long z){
     6     return (x-y)*(x-y)+(x-z)*(x-z)+(y-z)*(y-z);
     7 }
     8 int main(){
     9     ios::sync_with_stdio(false);
    10     cin.tie(NULL);
    11     cout.tie(NULL);
    12     int t;
    13     cin>>t;
    14     while(t--){
    15         int nr,ng,nb;
    16         cin>>nr>>ng>>nb;
    17         for(int i=1;i<=nr;++i)
    18             cin>>r[i];
    19         sort(r+1,r+1+nr);
    20         nr=unique(r+1,r+1+nr)-r-1;
    21         for(int i=1;i<=ng;++i)
    22             cin>>g[i];
    23         sort(g+1,g+1+ng);
    24         ng=unique(g+1,g+1+ng)-g-1;
    25         for(int i=1;i<=nb;++i)
    26             cin>>b[i];
    27         sort(b+1,b+1+nb);
    28         nb=unique(b+1,b+1+nb)-b-1;
    29         long long res=4e18;
    30         for(int i=1;i<=nr;++i){
    31             long long x=r[i];
    32             int pos=lower_bound(g+1,g+1+ng,x)-g;
    33             for(int j=max(1,pos-1);j<=min(ng,pos+1);++j){
    34                 long long y=g[j];
    35                 int temp=lower_bound(b+1,b+1+nb,(x+y)/2)-b;
    36                 if(temp>nb)
    37                     temp=nb;
    38                 for(int k=max(1,temp-1);k<=min(nb,temp+1);++k){
    39                     long long z=b[k];
    40                     res=min(res,func(x,y,z));
    41                 }
    42             }
    43             int pos2=lower_bound(b+1,b+1+nb,x)-b;
    44             for(int j=max(1,pos2-1);j<=min(nb,pos2+1);++j){
    45                 long long y=b[j];
    46                 int temp2=lower_bound(g+1,g+1+ng,(x+y)/2)-g;
    47                 if(temp2>ng)
    48                     temp2=ng;
    49                 for(int k=max(1,temp2-1);k<=min(ng,temp2+1);++k){
    50                     long long z=g[k];
    51                     res=min(res,func(x,y,z));
    52                 }
    53             }
    54         }
    55         cout<<res<<"
    ";
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    Spring1()
    常用的比较器:实现方式Compareable和Comparator
    数据结构 2(数据结构 逻辑关系 存储关系 数据类型 抽象数据类型)
    面试题目
    数据结构(1术语)
    第一次作业-四则运算题目生成程序
    第二次 作业——APP案例分析
    面试题
    字符串转换时间的方法
    安卓获取手机内存,SD卡内存使用状态的方法
  • 原文地址:https://www.cnblogs.com/ldudxy/p/12711808.html
Copyright © 2011-2022 走看看