1651C - Fault-tolerant Network - CodeForces Solution


brute force data structures implementation *1500

Please click on ads to support us..

Python Code:

R=lambda:[*map(int,input().split())]
for _ in[0]*R()[0]:R();a=R();b=R();print(min(sum(min(abs(c[i]-d[i]),sum(min(abs(u[i]-z)for
z in v)for u,v in((c,d),(d,c))))for i in(0,-1))for
c,d in((a,b),(a,b[::-1]))))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define foru(i, a, b) for (int i = a; i <= b; ++i)
#define ford(i, a, b) for (int i = a; i >= b; --i)
#define endl '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define bit(x, k) (((x) >> (k)) & 1ll)
#define on(x, k) ((x) | (1ll << (k)))
#define off(x, k) ((x) & (~(1ll << (k))))
#define ms(x, y) memset(x, y, sizeof(x))
const ll mod = 1e9 + 7;
const ll base = 31;
const ll maxn = 2e5 + 5;
void solve()
{
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    int dau1 = a.front(), cuoi1 = a.back();
    // a.pop_back();
    // reverse(all(a));
    // a.pop_back();
    // reverse(all(a));
    int dau2 = b.front(), cuoi2 = b.back();
    // b.pop_back();
    // reverse(all(b));
    // b.pop_back();
    // reverse(all(b));
    sort(all(a));
    sort(all(b));
    map<ll, ll> opt;
    auto it = lower_bound(all(b), dau1);
    if (it == b.end())
        --it;
    if (it == b.begin())
        opt[dau1] = abs(*it - dau1);
    else
        opt[dau1] = min(abs(*it - dau1), abs(*(it - 1) - dau1));

    it = lower_bound(all(b), cuoi1);
    if (it == b.end())
        --it;
    if (it == b.begin())
        opt[cuoi1] = abs(*it - cuoi1);
    else
        opt[cuoi1] = min(abs(*it - cuoi1), abs(*(it - 1) - cuoi1));

    it = lower_bound(all(a), dau2);
    if (it == a.end())
        --it;
    if (it == a.begin())
        opt[dau2] = abs(*it - dau2);
    else
        opt[dau2] = min(abs(*it - dau2), abs(*(it - 1) - dau2));

    it = lower_bound(all(a), cuoi2);
    if (it == a.end())
        --it;
    if (it == a.begin())
        opt[cuoi2] = abs(*it - cuoi2);
    else
        opt[cuoi2] = min(abs(*it - cuoi2), abs(*(it - 1) - cuoi2));
    ll res1 = min(abs(dau1 - dau2) + abs(cuoi1 - cuoi2), abs(dau1 - cuoi2) + abs(cuoi1 - dau2));
    ll res2 = min({abs(dau1 - dau2) + opt[cuoi1] + opt[cuoi2], abs(dau1 - cuoi2) + opt[cuoi1] + opt[dau2], abs(cuoi1 - dau2) + opt[dau1] + opt[cuoi2], abs(cuoi1 - cuoi2) + opt[dau1] + opt[dau2]});
    ll res3 = opt[dau1] + opt[cuoi1] + opt[dau2] + opt[cuoi2];
    cout << min({res1, res2, res3}) << endl;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

400B - Inna and New Matrix of Candies
870B - Maximum of Maximums of Minimums
1600E - Array Game
1149A - Prefix Sum Primes
277A - Learning Languages
769A - Year of University Entrance
1738A - Glory Addicts
1738C - Even Number Addicts
1064B - Equations of Mathematical Magic
384A - Coder
1738B - Prefix Sum Addicts
1352D - Alice Bob and Candies
1316D - Nash Matrix
1548B - Integers Have Friends
348A - Mafia
315B - Sereja and Array
204A - Little Elephant and Interval
385B - Bear and Strings
114C - Grammar Lessons
1427A - Avoiding Zero
583A - Asphalting Roads
1358B - Maria Breaks the Self-isolation
828A - Restaurant Tables
1735A - Working Week
1735D - Meta-set
1735B - Tea with Tangerines
1735C - Phase Shift
1321C - Remove Adjacent
281B - Nearest Fraction
1043A - Elections