1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| void solve() {
i64 n, m, k, sum = 0; cin >> n >> m >> k;
vector<pll> a(n + 1); vector<i64> ans(n + 1, 0), pre(n + 1, 0), b(n + 1, 0); for(int i = 1; i <= n; i ++) { cin >> a[i].first; a[i].second = i; sum += a[i].first; }
if(n == m) { for(int i = 1; i <= n; i ++) { cout << 0 << " \n"[i == n]; } return; }
i64 tmp = k - sum;
sort(a.begin() + 1, a.begin() + 1 + n);
for(int i = 1; i <= n; i ++) { pre[i] = pre[i - 1] + a[i].first; }
for(int i = 1; i <= n; i ++) { b[i] = a[i].first; }
auto check = [&](i64 x, int id) -> bool { i64 res = a[id].first + x + 1; i64 l = n - m, r = n; while(l < r) {i64 mid = l + r + 1 >> 1; if(b[mid] < res) { l = mid; } else { r = mid - 1; } } if(res <= b[n - m]) { return false; } else { i64 y = l - n + m, tot; if(id <= n - m) { tot = res * y - (pre[l] - pre[n - m]); } else { tot = res * y - (pre[l] - pre[n - m - 1] - b[id]); } if(tmp - x >= tot) { return false; } else { return true; } } };
for(int i = 1; i <= n; i ++) { i64 l = 0, r = tmp; while(l < r) { i64 mid = l + r >> 1; if(check(mid, i)) { r = mid; } else { l = mid + 1; } } ans[a[i].second] = check(l, i) ? l : -1; } for(int i = 1; i <= n; i ++) { cout << ans[i] << " \n"[i == n]; }
|