Submission #518788

# Submission time Handle Problem Language Result Execution time Memory
518788 2022-01-24T15:53:34 Z Jeff12345121 Simfonija (COCI19_simfonija) C++14
110 / 110
124 ms 4860 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

#ifdef LOCAL
ifstream in("in.in");
ofstream out("out.out");
#else
#define in cin
#define out cout
#endif

const int nmax = 100005, inf = (1LL << 60);

int n, k;
vector<int> a, b, v, s;

const int MAX_X = 2000005;

int sum(int l, int r) {
    return s[r] - s[l - 1];
}
int sol = inf;
void solve() {
    int sep1 = 0, sep2 = n + 1;
    /// sep1 is the biggest point so that v[sep1] <= -x, so that number behind sep1
    /// is just the number of values that will have an x straight up added to them.
    /// sep2 is the smallest number such that v[sep2] >= 0, so that they will all get
    /// x added to them
    /// between sep1 and sep2, sum will get added number of numbers that are there * x -their sum

    /// we also get the top numbers from all of these.

    s[0] = 0;
    for (int i = 1; i <= n; i++) {
        s[i] = s[i - 1] + v[i];
    }

    auto f = [&](int nr, int x) {
        ///returns the max that you can take out if you take out the last x values
        ///from right.
        int Left = k - nr, cursum = 0, Right = n - nr + 1;
        if (nr > 0) {
            cursum += sum(Right, n) + nr * x;
        }
        if (Left > 0) {
            cursum += -sum(1, Left) - Left * x;
        }
        return cursum;
    };

    int nr = k;
    for (int x = MAX_X; x >= 0; x--) {
        while (sep1 <= n && v[sep1 + 1] <= -x) {
            sep1++;
        }
        while (sep2 > 1 && v[sep2 - 1] >= 0) {
            sep2--;
        }

        int curSum = 0;
        if (sep2 <= n + 1) {
            curSum += sum(sep2, n) + (n - sep2 + 1) * x;  /// right
        }
        if (sep1 >= 1) {  // there are SOME elements smaller than -x
            curSum += -sum(1, sep1) - sep1 * x;
        }
        if (sep1 < sep2 - 1) {
            curSum += (sep2 - sep1 - 1) * x + sum(sep1 + 1, sep2 - 1);
        }

        while(nr >= 1 && f(nr - 1, x) > f(nr , x)) {
            nr--;
        }

        int bonus = f(nr, x);

        sol = min(sol, curSum - bonus);
    }
}
int32_t main() {
    in >> n >> k;
    a.resize(n + 1);
    b.resize(n + 1);
    v.resize(n + 1);
    s.resize(n + 1);

    for (int i = 1; i <= n; i++) {
        in >> a[i];
    }

    for (int i = 1; i <= n; i++) {
        in >> b[i];
    }

    for (int i = 1; i <= n; i++) {
        v[i] = a[i] - b[i];
    }
    sort(v.begin() + 1, v.end());

    solve();

    for (int i = 1; i <= n; i++) {
        v[i] *= -1;
    }

    sort(v.begin() + 1, v.end());
    solve();

    out << sol << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 30 ms 204 KB Output is correct
2 Correct 25 ms 268 KB Output is correct
3 Correct 26 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 3404 KB Output is correct
2 Correct 92 ms 3420 KB Output is correct
3 Correct 98 ms 3424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 3416 KB Output is correct
2 Correct 91 ms 3424 KB Output is correct
3 Correct 93 ms 3404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 3428 KB Output is correct
2 Correct 93 ms 3428 KB Output is correct
3 Correct 94 ms 3420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 3420 KB Output is correct
2 Correct 111 ms 4816 KB Output is correct
3 Correct 107 ms 4856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 3528 KB Output is correct
2 Correct 109 ms 4784 KB Output is correct
3 Correct 124 ms 4856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 3416 KB Output is correct
2 Correct 103 ms 4828 KB Output is correct
3 Correct 116 ms 4856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 3428 KB Output is correct
2 Correct 106 ms 4816 KB Output is correct
3 Correct 122 ms 4856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 3404 KB Output is correct
2 Correct 113 ms 4852 KB Output is correct
3 Correct 119 ms 4852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 3424 KB Output is correct
2 Correct 107 ms 4860 KB Output is correct
3 Correct 109 ms 4856 KB Output is correct