Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #ifndef __DEBUG__
- #define dbg(...) 42
- #endif
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using vl = vector<ll>;
- using vi = vector<int>;
- int main(int argc, char **argv)
- {
- ll n, k;
- cin >> n >> k;
- vl xs(n), ys(n), xacc(n + 2), yacc(n + 2);
- for (int i = 0; i < n; ++i)
- cin >> xs[i] >> ys[i];
- sort(xs.begin(), xs.end()), sort(ys.begin(), ys.end());
- for (int i = 0; i < n; ++i)
- xacc[i + 1] = xacc[i] + xs[i], yacc[i + 1] = yacc[i] + ys[i];
- auto find_k = [](ll d, const vl &as, const vl &acc) {
- ll ans = LLONG_MAX, n = as.size();
- for (ll i = 0, j = 0; i < n; ++i) {
- while (j < i && as[i] - as[j] > d)
- ++j;
- ll cost = (acc[n] - acc[i + 1]) - (n - i - 1) * as[i];
- cost += max(0ll, j * (as[i] - d) - acc[j]);
- ans = min(ans, cost);
- }
- for (ll i = 0, j = 0; i < n; ++i) {
- while (j < n && as[j] - as[i] <= d)
- ++j;
- ll cost = i * as[i] - acc[i];
- cost += acc[n] - acc[j] - (n - j) * (as[i] + d);
- ans = min(ans, cost);
- }
- return ans;
- };
- ll lb = -1, ub = 2e9;
- while (lb + 1 < ub) {
- ll mid = (lb + ub) / 2;
- ll fx = find_k(mid, xs, xacc), fy = find_k(mid, ys, yacc);
- // dbg(k, mid, fx, fy);
- (fx + fy > k ? lb : ub) = mid;
- }
- cout << ub << endl;
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement