Submission #225197

# Submission time Handle Problem Language Result Execution time Memory
225197 2020-04-19T14:07:47 Z Vimmer Simfonija (COCI19_simfonija) C++14
110 / 110
57 ms 4584 KB
#include <bits/stdc++.h>

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 200005
#define MOD ll(998244353)

using namespace std;

typedef long long ll;

typedef long double ld;


int main()
{

    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    ll n, k;

    cin >> n >> k;

    ll a[n], b[n], pr[n + 1];

    pr[n] = 0;

    for (ll i = 0; i < n; i++) cin >> a[i];

    for (ll i = 0; i < n; i++) cin >> b[i];

    vector <ll> g(n);

    for (ll i = 0; i < n; i++) g[i] = a[i] - b[i];

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

    g.pb(1e18);

    for (ll i = 0; i < n; i++)
    {
        pr[i] = 0;

        if (i != 0) pr[i] = pr[i - 1];

        pr[i] += g[i];
    }

    ll ans = 1e18, l = 0, r = n - k - 1;

    while (r < n)
    {
        ll mdr = (r + l + 1) / 2;

        ll mdl = mdr - 1;

        if (mdr >= 0 && mdr < n)
        {
            ll x = g[mdr];

            ll pos = upper_bound(g.begin(), g.end(), x) - g.begin();

            ll posr = lower_bound(g.begin(), g.end(), 0) - g.begin();

            if (x < 0)
            {
                ll sum = 0;

                if (posr <= r) sum = abs((r - max(posr, l) + 1) * x) + (pr[r] - (max(posr, l) == 0 ? 0 : pr[max(posr, l) - 1]));

                if (pos >= l) sum += abs(pr[min(pos - 1, r)] - (l == 0 ? 0 : pr[l - 1]) - x * (min(pos - 1, r) - l + 1));

                sum += pr[min(r, posr - 1)] - (max(pos, l) == 0 ? 0 : pr[max(pos, l) - 1]) + abs(x * (min(posr - 1, r) - max(pos, l) + 1));

                ans = min(ans, sum);
            }
            else
            {
                ll sum = 0;

                if (posr - 1 >= l) sum = (posr - l) * x + abs(pr[posr - 1] - (l == 0 ? 0 : pr[l - 1]));

                if (pos <= r) sum += pr[r] - (pos == 0 ? 0 : pr[pos - 1]) - (r - pos + 1) * x;

                sum += abs(pr[min(r, pos - 1)] - (max(posr, l) == 0 ? 0 : pr[max(posr, l) - 1]) - x * (min(r, pos - 1) - max(posr, l) + 1));

                ans = min(ans, sum);
            }
        }

        if (mdl >= 0 && mdl < n)
        {
            ll x = g[mdl];

            ll pos = upper_bound(g.begin(), g.end(), x) - g.begin();

            ll posr = lower_bound(g.begin(), g.end(), 0) - g.begin();

            if (x < 0)
            {
                ll sum = 0;

                if (posr <= r) sum = abs((r - max(posr, l) + 1) * x) + (pr[r] - (max(posr, l) == 0 ? 0 : pr[max(posr, l) - 1]));

                if (pos >= l) sum += abs(pr[min(pos - 1, r)] - (l == 0 ? 0 : pr[l - 1]) - x * (min(pos - 1, r) - l + 1));

                sum += pr[min(r, posr - 1)] - (max(pos, l) == 0 ? 0 : pr[max(pos, l) - 1]) + abs(x * (min(posr - 1, r) - max(pos, l) + 1));

                ans = min(ans, sum);
            }
            else
            {
                ll sum = 0;

                if (posr - 1 >= l) sum = (posr - l) * x + abs(pr[posr - 1] - (l == 0 ? 0 : pr[l - 1]));

                if (pos <= r) sum += pr[r] - (pos == 0 ? 0 : pr[pos - 1]) - (r - pos + 1) * x;

                sum += abs(pr[min(r, pos - 1)] - (max(posr, l) == 0 ? 0 : pr[max(posr, l) - 1]) - x * (min(r, pos - 1) - max(posr, l) + 1));

                ans = min(ans, sum);
            }
        }

        l++; r++;
    }

    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 3448 KB Output is correct
2 Correct 36 ms 3456 KB Output is correct
3 Correct 35 ms 3560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 3448 KB Output is correct
2 Correct 36 ms 3448 KB Output is correct
3 Correct 34 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 3456 KB Output is correct
2 Correct 36 ms 3584 KB Output is correct
3 Correct 34 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 3580 KB Output is correct
2 Correct 39 ms 4472 KB Output is correct
3 Correct 41 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 3448 KB Output is correct
2 Correct 57 ms 4472 KB Output is correct
3 Correct 48 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 3456 KB Output is correct
2 Correct 38 ms 3560 KB Output is correct
3 Correct 45 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 3456 KB Output is correct
2 Correct 41 ms 4472 KB Output is correct
3 Correct 46 ms 4584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 3448 KB Output is correct
2 Correct 55 ms 3560 KB Output is correct
3 Correct 40 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 3452 KB Output is correct
2 Correct 36 ms 4472 KB Output is correct
3 Correct 43 ms 4480 KB Output is correct