1336B - Xenia and Colorful Gems - CodeForces Solution


binary search greedy math sortings two pointers *1700

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long 
const int N=2e5+5;
const int M=1e9+7;



int32_t main(){
ios_base::sync_with_stdio(false);
    cin.tie(NULL); 

int tt;
cin>>tt;

while(tt--){

int nr,ng,nb;

cin>>nr>>ng>>nb;


vector<int>r(nr),g(ng),b(nb);


for(int i=0;i<nr;i++)
cin>>r[i];

for(int i=0;i<ng;i++)
cin>>g[i];

for(int i=0;i<nb;i++)
cin>>b[i];

sort(r.begin(),r.end());
sort(g.begin(),g.end());
sort(b.begin(),b.end());

int ans=LLONG_MAX;

for(int i=0;i<nr;i++){
    
    int z=r[i];
    
    vector<int>A,B;
    
    {
    auto it=upper_bound(g.begin(),g.end(),r[i]);
    
    if(it!=g.end()){
        A.push_back(*it);
    }
    if(it!=g.begin()){
        it--;
        A.push_back(*it);
    }
    }
    {
          auto it=upper_bound(b.begin(),b.end(),r[i]);
    
    if(it!=b.end()){
        B.push_back(*it);
    }
    if(it!=b.begin()){
        it--;
        B.push_back(*it);
    }
    }
    
    for(auto x:A){
        for(auto y:B){
          //  cout<<z<<" "<<x<<" "<<y<<"\n";
            int now=(x-y)*(x-y)+(y-z)*(y-z)+(x-z)*(x-z);
            ans=min(ans,now);
        }
    }
    
}

swap(nr,ng);
swap(r,g);

for(int i=0;i<nr;i++){
    
    int z=r[i];
    
    vector<int>A,B;
    
    {
    auto it=upper_bound(g.begin(),g.end(),r[i]);
    
    if(it!=g.end()){
        A.push_back(*it);
    }
    if(it!=g.begin()){
        it--;
        A.push_back(*it);
    }
    }
    {
          auto it=upper_bound(b.begin(),b.end(),r[i]);
    
    if(it!=b.end()){
        B.push_back(*it);
    }
    if(it!=b.begin()){
        it--;
        B.push_back(*it);
    }
    }
    
    for(auto x:A){
        for(auto y:B){
          //  cout<<z<<" "<<x<<" "<<y<<"\n";
            int now=(x-y)*(x-y)+(y-z)*(y-z)+(x-z)*(x-z);
            ans=min(ans,now);
        }
    }
    
}

swap(nr,nb);
swap(r,b);

for(int i=0;i<nr;i++){
    
    int z=r[i];
    
    vector<int>A,B;
    
    {
    auto it=upper_bound(g.begin(),g.end(),r[i]);
    
    if(it!=g.end()){
        A.push_back(*it);
    }
    if(it!=g.begin()){
        it--;
        A.push_back(*it);
    }
    }
    {
          auto it=upper_bound(b.begin(),b.end(),r[i]);
    
    if(it!=b.end()){
        B.push_back(*it);
    }
    if(it!=b.begin()){
        it--;
        B.push_back(*it);
    }
    }
    
    for(auto x:A){
        for(auto y:B){
          //  cout<<z<<" "<<x<<" "<<y<<"\n";
            int now=(x-y)*(x-y)+(y-z)*(y-z)+(x-z)*(x-z);
            ans=min(ans,now);
        }
    }
    
}


cout<<ans<<"\n";
}
}











Comments

Submit
0 Comments
More Questions

1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall
377A - Maze
500A - New Year Transportation
908D - New Year and Arbitrary Arrangement
199A - Hexadecimal's theorem
519C - A and B and Team Training
631A - Interview
961B - Lecture Sleep
522A - Reposts
1166D - Cute Sequences
1176A - Divide it
1527A - And Then There Were K
1618E - Singers' Tour
1560B - Who's Opposite