https://codeforces.com/contest/1337/problem/D

给出三个数组的大小,nrngnb( ≤ nrngn≤ 105,再给出三个数组,

r1r2rnr  (≤  ri  ≤ 10) ;

g1g2gng  (≤  gi  ≤ 109);

b1b2bnb  (≤ b≤ 109);

分别从三个数组中找到一个数,x,y,z,使 (xy)2+(yz)2+(zx)2 的值最小,求最小值;

思路:对于三个数组,每个数组 for 一遍二分找到另两个数组中和当前这个数最接近的数,然后计算就好了。

(思路很简单,但我的代码贼丑,比赛的时候写了很久,最后调完时间没了,再多一分钟就好了,第二天提交就过了,太气了!!!也怪我没有迅速的写,要是过了应该能加个几十分,,,呜呜呜呜,,,

#include <bits/stdc++.h>
using namespace std;
const int MAXN=4e5+5;
const int mod=1e9+7;
typedef long long ll;
const int inf=0x3f3f3f3f;
const long long INF=0x3f3f3f3f3f3f3f3f;
ll a[MAXN],b[MAXN],c[MAXN];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
        for(int i=1;i<=m;i++)scanf("%lld",&b[i]);
        for(int i=1;i<=k;i++)scanf("%lld",&c[i]);
        sort(a+1,a+1+n);
        sort(b+1,b+1+m);
        sort(c+1,c+1+k);
        ll ans=INF;
        for(int i=1;i<=n;i++)
        {
            ll aa=a[i];
            ll b0=lower_bound(b+1,b+1+m,a[i])-b;
            ll bb=b[b0];
            ll c0=lower_bound(c+1,c+1+k,a[i])-c;
            ll cc=c[c0];
            ll tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);
            if(b0-1>=1||c0-1>=1)
            {
                if(b0-1>=1&&c0-1>=1)
                {
                    bb=b[b0-1];
                    cc=c[c0-1];
                    tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
                    ans=min(ans,tmp);
                }
                if((b0-1>=1&&c0-1>=1)||b0-1<1)
                {
                    bb=b[b0];
                    cc=c[c0-1];
                    tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
                    ans=min(ans,tmp);
                }
                if((b0-1>=1&&c0-1>=1)||c0-1<1)
                {
                    bb=b[b0-1];
                    cc=c[c0];
                    tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
                    ans=min(ans,tmp);
                }
            }
        }

        for(int i=1;i<=m;i++)
        {
            ll bb=b[i];
            ll a0=lower_bound(a+1,a+1+n,b[i])-a;
            ll aa=a[a0];
            ll c0=lower_bound(c+1,c+1+k,b[i])-c;
            ll cc=c[c0];
            ll tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);
            if(a0-1>=1||c0-1>=1)
            {
                if(a0-1>=1&&c0-1>=1)
                {
                    aa=a[a0-1];
                    cc=c[c0-1];
                    tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
                    ans=min(ans,tmp);
                }
                if((a0-1>=1&&c0-1>=1)||a0-1<1)
                {
                    aa=a[a0];
                    cc=c[c0-1];
                    tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
                    ans=min(ans,tmp);
                }
                if((a0-1>=1&&c0-1>=1)||c0-1<1)
                {
                    aa=a[a0-1];
                    cc=c[c0];
                    tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
                    ans=min(ans,tmp);
                }

            }
        }
        for(int i=1;i<=k;i++)
        {
            ll cc=c[i];
            ll b0=lower_bound(b+1,b+1+m,c[i])-b;
            ll bb=b[b0];
            ll a0=lower_bound(a+1,a+1+n,c[i])-a;
            ll aa=a[a0];
            ll tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);
            if(a0-1>=1||b0-1>=1)
            {
                if(a0-1>=1&&b0-1>=1)
                {
                    bb=b[b0-1];
                    aa=a[a0-1];
                    tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
                    ans=min(ans,tmp);
                }
                if((a0-1>=1&&b0-1>=1)||b0-1<1)
                {
                    bb=b[b0];
                    aa=a[a0-1];
                    tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
                    ans=min(ans,tmp);
                }
                if((a0-1>=1&&b0-1>=1)||a0-1<1)
                {
                    bb=b[b0-1];
                    aa=a[a0];
                    tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
                    ans=min(ans,tmp);
                }

            }
        }
        printf("%lld\n",ans);

        for(int i=1;i<=n;i++)a[i]=0;//多组记得初始化
        for(int i=1;i<=m;i++)b[i]=0;
        for(int i=1;i<=k;i++)c[i]=0;
    }
    return 0;
}
View Code

 

#include <bits/stdc++.h>
using namespace std;
const int MAXN=4e5+5;
const int mod=1e9+7;
typedef long long ll;
const int inf=0x3f3f3f3f;
const long long INF=0x3f3f3f3f3f3f3f3f;
ll a[MAXN],b[MAXN],c[MAXN];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        a[0]=-1e9;//把下标为0的数设为很小的一个数,对答案就没有影响了,不用讨论下标问题
        b[0]=-1e9;
        c[0]=-1e9;
        for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
        for(int i=1;i<=m;i++)scanf("%lld",&b[i]);
        for(int i=1;i<=k;i++)scanf("%lld",&c[i]);
        sort(a+1,a+1+n);
        sort(b+1,b+1+m);
        sort(c+1,c+1+k);
        ll ans=INF;
        for(int i=1;i<=n;i++)
        {
            ll aa=a[i];
            ll b0=lower_bound(b+1,b+1+m,a[i])-b;
            ll bb=b[b0];
            ll c0=lower_bound(c+1,c+1+k,a[i])-c;
            ll cc=c[c0];
            ll tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);

            bb=b[b0-1];
            cc=c[c0-1];
            tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);

            bb=b[b0];
            cc=c[c0-1];
            tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);

            bb=b[b0-1];
            cc=c[c0];
            tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);

        }

        for(int i=1;i<=m;i++)
        {
            ll bb=b[i];
            ll a0=lower_bound(a+1,a+1+n,b[i])-a;
            ll aa=a[a0];
            ll c0=lower_bound(c+1,c+1+k,b[i])-c;
            ll cc=c[c0];
            ll tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);

            aa=a[a0-1];
            cc=c[c0-1];
            tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);

            aa=a[a0];
            cc=c[c0-1];
            tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);

            aa=a[a0-1];
            cc=c[c0];
            tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);

        }
        for(int i=1;i<=k;i++)
        {
            ll cc=c[i];
            ll b0=lower_bound(b+1,b+1+m,c[i])-b;
            ll bb=b[b0];
            ll a0=lower_bound(a+1,a+1+n,c[i])-a;
            ll aa=a[a0];
            ll tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);
            
            bb=b[b0-1];
            aa=a[a0-1];
            tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);

            bb=b[b0];
            aa=a[a0-1];
            tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);

            bb=b[b0-1];
            aa=a[a0];
            tmp=(aa-bb)*(aa-bb)+(cc-bb)*(cc-bb)+(aa-cc)*(aa-cc);
            ans=min(ans,tmp);

        }
        printf("%lld\n",ans);

        for(int i=1;i<=n;i++)a[i]=0;//多组记得初始化
        for(int i=1;i<=m;i++)b[i]=0;
        for(int i=1;i<=k;i++)c[i]=0;
    }
    return 0;
}
View Code

 

posted on 2020-04-17 12:32  MZRONG  阅读(262)  评论(0编辑  收藏  举报