Codeforces Round #635 (Div. 2)A~E

D. Xenia and Colorful Gems    题目链接

思路:

数组混一起排序后,枚举每个点作为中间值,找前后离他最近的其他数组的值,从而计算最大值。

#include <bits/stdc++.h>
using namespace std;
const int maxn =  3e5 + 10;
#define ll long long
#define ios std::ios::sync_with_stdio(false)
const ll INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
#define int long long
typedef unsigned long long ULL;
struct node
{
    int x , no;
};
bool cmp(node a , node b)
{
    return a.x < b.x;
}
vector<node>a;
int pre[maxn][4];
int nex[maxn][4];
int check(int aa , int bb , int cc)
{
    return ((aa - bb) * (aa - bb) + (aa - cc) * (aa - cc) + (bb - cc) * (bb - cc));
}
signed main()
{
    ios;
    cin.tie(0);///
    int t;
    cin >> t;
    while(t --){
        a.clear();
        int r , g , b;
        cin >> r >> g >> b;
        for(int i = 1 ; i <= r ; i ++){
            node t;
            cin >> t.x;
            t.no = 1;
            a.push_back(t);
        }
        for(int i = 1 ; i <= g ; i ++){
            node t;
            cin >> t.x;
            t.no = 2;
            a.push_back(t);
        }
        for(int i = 1 ; i <= b ; i ++){
            node t;
            cin >> t.x;
            t.no = 3;
            a.push_back(t);
        }
        sort(a.begin() , a.end() , cmp);
        int one = -1 , two = -1 , second = -1;
        for(int i = 0 ; i < a.size() ; i ++){
            if(a[i].no == 1){
                one = i;
            }
            else if(a[i].no == 2){
                two = i;
            }
            else{
                second = i;
            }
            pre[i][1] = one , pre[i][2] = two , pre[i][3] = second;
        }
        one = -1 , two = -1 , second = -1;
        for(int i = a.size() - 1 ; i >= 0 ; i --){
            if(a[i].no == 1){
                one = i;
            }
            else if(a[i].no == 2){
                two = i;
            }
            else{
                second = i;
            }
            nex[i][1] = one , nex[i][2] = two , nex[i][3] = second;
        }
        int ans = INF;
        for(int i = 0 ; i < a.size() ; i ++){
            if(a[i].no == 1){
                if(pre[i][2] != -1 && nex[i][3] != -1){
                    ans = min(ans , check(a[i].x , a[pre[i][2]].x , a[nex[i][3]].x));
                }
                if(pre[i][3] != -1 && nex[i][2] != -1){
                    ans = min(ans , check(a[i].x , a[pre[i][3]].x , a[nex[i][2]].x));
                }
            }
            else if(a[i].no == 2){
                if(pre[i][1] != -1 && nex[i][3] != -1){
                    ans = min(ans , check(a[i].x , a[pre[i][1]].x , a[nex[i][3]].x));
                }
                if(pre[i][3] != -1 && nex[i][1] != -1){
                    ans = min(ans , check(a[i].x , a[pre[i][3]].x , a[nex[i][1]].x));
                }
            }
            else{
                if(pre[i][1] != -1 && nex[i][2] != -1){
                    ans = min(ans , check(a[i].x , a[pre[i][1]].x , a[nex[i][2]].x));
                }
                if(pre[i][2] != -1 && nex[i][1] != -1){
                    ans = min(ans , check(a[i].x , a[pre[i][2]].x , a[nex[i][1]].x));
                }
            }
        }
        cout << ans << '\n';
    }
    return 0;
}
View Code

 

posted @ 2020-04-17 01:22  GoodVv  阅读(199)  评论(0编辑  收藏  举报