Submission #22995030


Source Code Expand

#include<bits/stdc++.h>
using namespace std;

// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")

/* // Ordered Set
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)
*/

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
#define f0(i,a,b) for(int i=a;i<b;i++)
#define f1(i,a,b) for(int i=a;i<=b;i++)
#define f2(i,a,b) for(int i=a;i>b;i--)
#define f3(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
// or add ll at the end for long long
#define cntleadz(x) __builtin_clz(x)
#define cnttrailz(x) __builtin_ctz(x)
#define cntpop(x) __builtin_popcount(x)
#define binparity(x) __builtin_parity(x)
#define pb push_back
#define pii pair<int,int>
#define int long long
#define fi first
#define se second
#define mod 1000000007
// #define mod 998244353
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)

ll binExp(ll x,ll n,ll m)
{
    ll res=1;
    while(n)
    {
        if(n&1) res=(res*x)%m;
        x=(x*x)%m;
        n>>=1;
    }
    return res;
}

ll modInv(ll i, ll m) {return binExp(i,m-2,m);}
ll add(ll a, ll b) {ll res = a+b; if(res >= mod) res -= mod; if(res < 0) res += mod; return res;}
ll mul(ll a, ll b) {ll res = (a)*(b); res %= mod; if(res < 0) res += mod; return res;}
// ll fact[1000001];
// ll ncr(int n,int r) {return (n>=r?(fact[n]*modInv(fact[r],mod))%mod*modInv(fact[n-r],mod)%mod:0);}

// https://codeforces.com/contest/558/problem/E
// Do this question via the current method as
// well as SegTree with Lazy propagation



void solve()
{
    int n;
    cin>>n;

    char col;
    int x;
    vector<int> r,g,b;
    f0(i,0,2*n)
    {
        cin>>x>>col;
        if(col == 'R') r.pb(x);
        else if(col == 'G') g.pb(x);
        else b.pb(x);
    }

    sort(all(r));
    sort(all(g));
    sort(all(b));

    if(r.size()%2 == 0 && g.size()%2 == 0 && b.size()%2 == 0)
    {
        cout<<"0\n";
        return;
    }

    if(r.size()%2==0)
    {
        swap(b,r);
    }
    else if(g.size()%2==0)
    {
        swap(g,b);
    }

    // now r and g are the odd ones
    // cout<<r.size()<<' '<<g.size()<<' '<<b.size()<<'\n';

    int ans = LLONG_MAX;

    for(int x : r)
    {
        auto it = lower_bound(all(g),x);
        if(it != g.end())
        {
            ans = min(ans, abs(x-*it));
        }
        if(it != g.begin())
        {
            ans = min(ans, abs(x-*prev(it)));
        }
    }

    int mnR[b.size()];
    int mnG[b.size()];

    f0(i,0,b.size())
    {
        auto it = lower_bound(all(r),b[i]);
        int mn = LLONG_MAX;
        if(it != r.end())
        {
            mn = min(mn, abs(b[i]-*it));
        }
        if(it != r.begin())
        {
            mn = min(mn, abs(b[i]-*prev(it)));
        }
        mnR[i] = mn;
        mn = LLONG_MAX;
        it = lower_bound(all(g),b[i]);
        if(it != g.end())
        {
            mn = min(mn, abs(b[i]-*it));
        }
        if(it != g.begin())
        {
            mn = min(mn, abs(b[i]-*prev(it)));
        }
        mnG[i] = mn;
    }

    f0(i,1,b.size())
    {
        ans = min(ans, mnR[i]+mnG[i-1]);
        ans = min(ans, mnG[i]+mnR[i-1]);
        mnG[i] = min(mnG[i], mnG[i-1]);
        mnR[i] = min(mnR[i], mnR[i-1]);
    }
    cout<<ans<<'\n';
}

signed main()
{
    fast

    clock_t start,end;
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    start = clock();

    int tc;
    tc = 1;
    // cin>>tc;
    f1(i,1,tc)
        solve();

    end = clock();
    double time_taken = double(end-start) / double(CLOCKS_PER_SEC);
    // cout<<time_taken<<" sec";
}

Submission Info

Submission Time
Task B - RGB Matching
User yk_ax
Language C++ (GCC 9.2.1)
Score 500
Code Size 4280 Byte
Status AC
Exec Time 59 ms
Memory 6328 KiB

Compile Error

./Main.cpp: In function ‘void solve()’:
./Main.cpp:23:32: warning: comparison of integer expressions of different signedness: ‘long long int’ and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   23 | #define f0(i,a,b) for(int i=a;i<b;i++)
......
  124 |     f0(i,0,b.size())
      |        ~~~~~~~~~~~~             
./Main.cpp:124:5: note: in expansion of macro ‘f0’
  124 |     f0(i,0,b.size())
      |     ^~
./Main.cpp:23:32: warning: comparison of integer expressions of different signedness: ‘long long int’ and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   23 | #define f0(i,a,b) for(int i=a;i<b;i++)
......
  150 |     f0(i,1,b.size())
      |        ~~~~~~~~~~~~             
./Main.cpp:150:5: note: in expansion of macro ‘f0’
  150 |     f0(i,1,b.size())
      |     ^~
./Main.cpp: In function ‘int main()’:
./Main.cpp:178:12: warning: unused variable ‘time_taken’ [-Wunused-variable]
  178 |     double time_taken = double(end-start) / double(CLOCKS_PER_SEC);
      |            ^~~~~~~~~~

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 34 ms 5500 KiB
hand_02.txt AC 28 ms 5528 KiB
hand_03.txt AC 24 ms 5552 KiB
hand_04.txt AC 27 ms 5552 KiB
random2_01.txt AC 51 ms 6088 KiB
random2_02.txt AC 53 ms 5912 KiB
random2_03.txt AC 54 ms 6116 KiB
random2_04.txt AC 51 ms 5964 KiB
random2_05.txt AC 53 ms 6024 KiB
random2_06.txt AC 13 ms 3720 KiB
random2_07.txt AC 16 ms 4092 KiB
random2_08.txt AC 23 ms 4412 KiB
random2_09.txt AC 49 ms 5956 KiB
random2_10.txt AC 22 ms 4380 KiB
random2_11.txt AC 2 ms 3524 KiB
random2_12.txt AC 2 ms 3528 KiB
random2_13.txt AC 2 ms 3520 KiB
random2_14.txt AC 2 ms 3616 KiB
random2_15.txt AC 2 ms 3576 KiB
random_01.txt AC 51 ms 5240 KiB
random_02.txt AC 58 ms 6328 KiB
random_03.txt AC 49 ms 5220 KiB
random_04.txt AC 58 ms 5968 KiB
random_05.txt AC 48 ms 5272 KiB
random_06.txt AC 57 ms 6292 KiB
random_07.txt AC 59 ms 5992 KiB
random_08.txt AC 56 ms 6004 KiB
random_09.txt AC 57 ms 6088 KiB
random_10.txt AC 55 ms 5948 KiB
random_11.txt AC 58 ms 6316 KiB
random_12.txt AC 58 ms 5752 KiB
random_13.txt AC 55 ms 6244 KiB
random_14.txt AC 49 ms 5512 KiB
random_15.txt AC 57 ms 6280 KiB
random_16.txt AC 57 ms 6048 KiB
random_17.txt AC 57 ms 6080 KiB
random_18.txt AC 57 ms 6040 KiB
random_19.txt AC 58 ms 6168 KiB
random_20.txt AC 56 ms 6160 KiB
random_21.txt AC 17 ms 3920 KiB
random_22.txt AC 7 ms 3800 KiB
random_23.txt AC 22 ms 3912 KiB
random_24.txt AC 24 ms 4408 KiB
random_25.txt AC 17 ms 4040 KiB
random_26.txt AC 22 ms 4084 KiB
random_27.txt AC 17 ms 4032 KiB
random_28.txt AC 46 ms 5280 KiB
random_29.txt AC 48 ms 5504 KiB
random_30.txt AC 43 ms 5328 KiB
random_31.txt AC 39 ms 4652 KiB
random_32.txt AC 27 ms 4332 KiB
random_33.txt AC 19 ms 3976 KiB
random_34.txt AC 10 ms 3848 KiB
random_35.txt AC 21 ms 4024 KiB
random_36.txt AC 47 ms 5200 KiB
random_37.txt AC 38 ms 5000 KiB
random_38.txt AC 25 ms 4368 KiB
random_39.txt AC 38 ms 4980 KiB
random_40.txt AC 45 ms 5320 KiB
random_41.txt AC 2 ms 3524 KiB
random_42.txt AC 2 ms 3568 KiB
random_43.txt AC 3 ms 3496 KiB
random_44.txt AC 2 ms 3608 KiB
random_45.txt AC 2 ms 3560 KiB
random_46.txt AC 2 ms 3556 KiB
random_47.txt AC 2 ms 3508 KiB
random_48.txt AC 2 ms 3508 KiB
random_49.txt AC 2 ms 3640 KiB
random_50.txt AC 2 ms 3456 KiB
random_51.txt AC 1 ms 3540 KiB
random_52.txt AC 2 ms 3684 KiB
random_53.txt AC 2 ms 3508 KiB
random_54.txt AC 2 ms 3604 KiB
random_55.txt AC 2 ms 3656 KiB
random_56.txt AC 2 ms 3484 KiB
random_57.txt AC 2 ms 3476 KiB
random_58.txt AC 2 ms 3572 KiB
random_59.txt AC 2 ms 3532 KiB
random_60.txt AC 1 ms 3540 KiB
sample_01.txt AC 2 ms 3656 KiB
sample_02.txt AC 2 ms 3428 KiB
sample_03.txt AC 2 ms 3436 KiB