1909C - Heavy Intervals - CodeForces Solution


greedy math sortings

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define fast {ios_base::sync_with_stdio(false); cin.tie(0);}
typedef long long ll;
#define int long long
#define float long double
int cs;
void solve()
{
    int n,ans=0;;
    cin >> n;
    vector<int> a(n), b(n), c;
    set<int> st;
    for(int i=0;i<n;i++)cin >> a[i];
    for(int k,i=0;i<n;i++) cin >> k, st.insert(k);
    for(int i=0;i<n;i++) cin >> b[i];
    sort(a.rbegin(),a.rend());
    for(int i=0;i<n;i++)
    {
        auto it=*st.upper_bound(a[i]);
        c.push_back(it-a[i]);
        st.erase(it);
    }
    sort(c.begin(), c.end());
    sort(b.rbegin(),b.rend());

    for(int i=0;i<n;i++) ans+=c[i]*b[i];
    cout << ans << endl;
}
signed main()
{
    fast
    int t=1;
    cin >> t;
    for(cs=1;cs<=t;cs++)
    {
        //cout << "Case " << cs << ": "; 
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree