Submission #22996626


Source Code Expand

//clear adj and visited vector declared globally after each test case
//check for long long overflow
//while adding and subs check if mod becomes -ve
//while using an integer directly in a builtin function add ll
//Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
//Dont keep array name as size or any other key word
//ctrl+end - to break infinite loop in sublime text
 
#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
using namespace std; 
#define Fio                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(nullptr);                 \
    cout.tie(nullptr)
#define ll long long int
#define fo(i, a, b) for (long long int i = a; i <= b; i++)
#define w(t)  \
    int t;    \
    cin >> t; \
    while (t--)
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define vl vector<long long int>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long int>>
#define mii map<int, int>
#define umii unordered_map<int, int>
#define mll map<ll,ll>
#define umll unordered_map<ll,ll>
#define newl cout<<"\n"
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const ll inf = 1e9+7;
const ll mod = 998244353 ;
#define MAX 200005 

void call_priyanshu(){
    Fio;
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt", "w", stdout);
    #endif
}

ll mini(ll a,ll b){
    if(a>=b)return b;
    return a;
}
 
ll maxi(ll a,ll b){
    if(a>=b)return a;
    return b;
}

ll calc(vl &A,vl &B,ll n,ll m){
    ll ans=1e18;
    fo(i,0,n-1){
        auto b = lower_bound(all(B),A[i]);
        if(b!=B.end())ans = mini(ans,abs(*b-A[i]));
        auto bb = upper_bound(all(B),A[i]);
        if(bb!=B.end())ans = mini(ans,abs(*bb-A[i]));
    }
    fo(i,0,m-1){
        auto b = lower_bound(all(A),B[i]);
        if(b!=A.end())ans = mini(ans,abs(*b-B[i]));
        auto bb = upper_bound(all(A),B[i]);
        if(bb!=A.end())ans = mini(ans,abs(*bb-B[i]));
    }
    return ans;
}

ll calc2(vl &A,vl &B,vl &C,ll n,ll m,ll nn){
    ll a1 = calc(A,B,n,m);
    ll a2 = calc(A,C,n,nn);
    return (a1+a2);
}

void solve(){
    //write your code here
    ll n;
    cin>>n;
    vl B,R,G;
    fo(i,0,2*n-1){
        ll v;
        char c;
        cin>>v>>c;
        if(c=='B'){
            B.pb(v);
        }
        if(c=='R'){
            R.pb(v);
        }
        if(c=='G'){
            G.pb(v);
        }
    }
    sort(all(B));
    sort(all(R));
    sort(all(G));
    ll b = B.size(),r=R.size(),g = G.size();
    if(b%2==0 && r%2==0 && g%2==0){
        cout<<"0";
        return;
    }
    ll ans = 0,ans2 = 0;
    if(b%2==0){
        ans = calc(R,G,r,g);
        if(b!=0){
            ans2 = calc2(B,R,G,b,r,g);
            ans = mini(ans,ans2);
        }
    }
    if(r%2==0){
        ans = calc(B,G,b,g);
        if(r!=0){
            ans2 = calc2(R,B,G,r,b,g);
            ans = mini(ans,ans2);
        }   
    }
    if(g%2==0){
        ans = calc(B,R,b,r);
        if(g!=0){
            ans2 = calc2(G,B,R,g,b,r);
            ans = mini(ans,ans2);
        }
    }
    cout<<ans;
}


int main(){
    call_priyanshu();
    // w(T){
        solve();
    // }
    return 0;
}

Submission Info

Submission Time
Task B - RGB Matching
User loop_oops
Language C++ (GCC 9.2.1)
Score 500
Code Size 3325 Byte
Status AC
Exec Time 80 ms
Memory 5732 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 82
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, random2_01.txt, random2_02.txt, random2_03.txt, random2_04.txt, random2_05.txt, random2_06.txt, random2_07.txt, random2_08.txt, random2_09.txt, random2_10.txt, random2_11.txt, random2_12.txt, random2_13.txt, random2_14.txt, random2_15.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, random_37.txt, random_38.txt, random_39.txt, random_40.txt, random_41.txt, random_42.txt, random_43.txt, random_44.txt, random_45.txt, random_46.txt, random_47.txt, random_48.txt, random_49.txt, random_50.txt, random_51.txt, random_52.txt, random_53.txt, random_54.txt, random_55.txt, random_56.txt, random_57.txt, random_58.txt, random_59.txt, random_60.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 30 ms 4104 KiB
hand_02.txt AC 27 ms 4236 KiB
hand_03.txt AC 29 ms 4160 KiB
hand_04.txt AC 27 ms 4216 KiB
random2_01.txt AC 67 ms 5476 KiB
random2_02.txt AC 67 ms 5360 KiB
random2_03.txt AC 67 ms 5468 KiB
random2_04.txt AC 67 ms 5128 KiB
random2_05.txt AC 64 ms 5472 KiB
random2_06.txt AC 12 ms 3660 KiB
random2_07.txt AC 24 ms 3892 KiB
random2_08.txt AC 33 ms 3992 KiB
random2_09.txt AC 62 ms 4896 KiB
random2_10.txt AC 32 ms 4064 KiB
random2_11.txt AC 2 ms 3608 KiB
random2_12.txt AC 2 ms 3564 KiB
random2_13.txt AC 2 ms 3588 KiB
random2_14.txt AC 3 ms 3508 KiB
random2_15.txt AC 2 ms 3604 KiB
random_01.txt AC 51 ms 5272 KiB
random_02.txt AC 80 ms 5500 KiB
random_03.txt AC 49 ms 5212 KiB
random_04.txt AC 79 ms 5308 KiB
random_05.txt AC 50 ms 5232 KiB
random_06.txt AC 78 ms 5404 KiB
random_07.txt AC 78 ms 5240 KiB
random_08.txt AC 78 ms 5276 KiB
random_09.txt AC 79 ms 5212 KiB
random_10.txt AC 80 ms 5216 KiB
random_11.txt AC 78 ms 5536 KiB
random_12.txt AC 79 ms 5456 KiB
random_13.txt AC 79 ms 5492 KiB
random_14.txt AC 49 ms 5400 KiB
random_15.txt AC 78 ms 5732 KiB
random_16.txt AC 79 ms 5272 KiB
random_17.txt AC 78 ms 5240 KiB
random_18.txt AC 78 ms 5176 KiB
random_19.txt AC 79 ms 5412 KiB
random_20.txt AC 79 ms 5244 KiB
random_21.txt AC 23 ms 3688 KiB
random_22.txt AC 9 ms 3596 KiB
random_23.txt AC 27 ms 3904 KiB
random_24.txt AC 40 ms 3792 KiB
random_25.txt AC 29 ms 3884 KiB
random_26.txt AC 28 ms 4032 KiB
random_27.txt AC 27 ms 3808 KiB
random_28.txt AC 61 ms 4504 KiB
random_29.txt AC 74 ms 4648 KiB
random_30.txt AC 61 ms 4456 KiB
random_31.txt AC 49 ms 4444 KiB
random_32.txt AC 38 ms 4012 KiB
random_33.txt AC 23 ms 3940 KiB
random_34.txt AC 13 ms 3716 KiB
random_35.txt AC 29 ms 4120 KiB
random_36.txt AC 64 ms 4516 KiB
random_37.txt AC 54 ms 4380 KiB
random_38.txt AC 41 ms 4052 KiB
random_39.txt AC 53 ms 4472 KiB
random_40.txt AC 63 ms 4436 KiB
random_41.txt AC 2 ms 3484 KiB
random_42.txt AC 2 ms 3580 KiB
random_43.txt AC 3 ms 3660 KiB
random_44.txt AC 3 ms 3584 KiB
random_45.txt AC 4 ms 3516 KiB
random_46.txt AC 3 ms 3512 KiB
random_47.txt AC 3 ms 3652 KiB
random_48.txt AC 2 ms 3504 KiB
random_49.txt AC 3 ms 3632 KiB
random_50.txt AC 2 ms 3648 KiB
random_51.txt AC 2 ms 3564 KiB
random_52.txt AC 2 ms 3540 KiB
random_53.txt AC 2 ms 3480 KiB
random_54.txt AC 2 ms 3524 KiB
random_55.txt AC 3 ms 3612 KiB
random_56.txt AC 1 ms 3548 KiB
random_57.txt AC 3 ms 3552 KiB
random_58.txt AC 3 ms 3556 KiB
random_59.txt AC 2 ms 3468 KiB
random_60.txt AC 3 ms 3540 KiB
sample_01.txt AC 2 ms 3416 KiB
sample_02.txt AC 2 ms 3620 KiB
sample_03.txt AC 2 ms 3620 KiB