답안 #225196

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
225196 2020-04-19T14:05:46 Z Vimmer Simfonija (COCI19_simfonija) C++14
44 / 110
49 ms 3736 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 3576 KB Output is correct
2 Correct 38 ms 3664 KB Output is correct
3 Correct 36 ms 3576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 3584 KB Output is correct
2 Correct 37 ms 3576 KB Output is correct
3 Correct 33 ms 3576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 3576 KB Output is correct
2 Correct 36 ms 3576 KB Output is correct
3 Correct 35 ms 3576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 33 ms 3584 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 45 ms 3644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 3688 KB Output is correct
2 Incorrect 40 ms 3576 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 46 ms 3576 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 3576 KB Output is correct
2 Incorrect 49 ms 3736 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 39 ms 3576 KB Output isn't correct
2 Halted 0 ms 0 KB -