Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define all(x) begin(x),end(x)
- #define make_unique(x) sort(all(x)); x.erase(unique(all(x)), end(x))
- using namespace std;
- using ll = long long;
- struct Note {
- int dlt;
- int pos;
- int index;
- explicit Note(int d, int p, int i) : dlt(d), pos(p), index(i) {}
- bool operator < (const Note& o) const {
- return dlt < o.dlt || dlt == o.dlt && index < o.index;
- }
- bool operator == (const Note& o) const {
- return index == o.index;
- }
- bool operator != (const Note& o) const {
- return index != o.index;
- }
- };
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n;
- cin >> n;
- vector<vector<int>> arr(n);
- vector<vector<int>> pref(n);
- for (int i = 0; i < n; i++) {
- int m;
- cin >> m;
- arr[i].resize(m);
- pref[i].resize(m);
- for (int& val : arr[i]) cin >> val;
- sort(all(arr[i]));
- pref[i] = arr[i];
- for (int j = 1; j < m; j++) {
- pref[i][j] += pref[i][j - 1];
- }
- }
- auto get = [&] (int i, int val) {
- int pos = lower_bound(all(arr[i]), val) - begin(arr[i]);
- int m = arr[i].size();
- if (pos == m) {
- return m * 1ll * val - pref[i].back();
- } else {
- ll ans = 0;
- if (pos) {
- ans += pos * 1ll * val - pref[i][pos - 1];
- }
- ans += pref[i].back() - (pos ? pref[i][pos - 1] : 0) - val * 1ll * (m - pos);
- return ans;
- }
- };
- int t;
- cin >> t;
- vector<int> best(n);
- vector<int> cnt_le(n);
- vector<int> cnt_h(n);
- ll cur_answer = 0;
- for (int i = 0; i < n; i++) {
- ll cur = 1e18;
- for (int j = 0; j < arr[i].size(); j++) {
- ll nxt = get(i, arr[i][j]);
- if (cur >= nxt) {
- cur = nxt;
- best[i] = arr[i][j];
- cnt_le[i] = i + 1;
- cnt_h[i] = arr[i].size() - 1 - i;
- }
- }
- cur_answer += cur;
- }
- int sum = accumulate(all(best), 0);
- if (sum <= t) {
- set<Note> st;
- for (int i = 0; i < n; i++) {
- int cur = get(i, best[i] + 1) - get(i, best[i]);
- Note note{cur, cnt_le[i] - 1, i};
- st.insert(note);
- }
- while (sum < t) {
- auto note = *st.begin();
- st.erase(st.begin());
- int i = note.index;
- int pos = note.pos;
- int dlt = note.dlt;
- if (pos + 1 == arr[i].size()) {
- best[i] += (t - sum);
- sum = t;
- } else {
- int seg = arr[i][pos + 1] - arr[i][pos];
- int used = min<int>(seg, t - sum);
- best[i] += used;
- sum += used;
- cnt_le[i]++;
- if (used == seg) {
- int cur = get(i, best[i] + 1) - get(i, best[i]);
- Note note{cur, cnt_le[i] - 1, i};
- st.insert(note);
- }
- }
- }
- for (int val : best) {
- cout << val << ' ';
- }
- cout << endl;
- } else {
- vector<int> best(n);
- vector<int> cnt_le(n);
- vector<int> cnt_h(n);
- ll cur_answer = 0;
- for (int i = 0; i < n; i++) {
- ll cur = 1e18;
- for (int j = 0; j < arr[i].size(); j++) {
- ll nxt = get(i, arr[i][j]);
- if (cur > nxt) {
- cur = nxt;
- best[i] = arr[i][j];
- cnt_le[i] = i + 1;
- cnt_h[i] = arr[i].size() - 1 - i;
- }
- }
- cur_answer += cur;
- }
- set<Note> st;
- for (int i = 0; i < n; i++) {
- int cur = get(i, best[i] - 1) - get(i, best[i]);
- Note note{cur, cnt_le[i] - 1, i};
- st.insert(note);
- }
- while (sum > t) {
- auto note = *st.begin();
- st.erase(st.begin());
- int i = note.index;
- int pos = note.pos;
- int dlt = note.dlt;
- if (pos == 0) {
- int d = min(best[i], sum - t);
- best[i] -= d;
- sum -= d;
- } else {
- int seg = arr[i][pos] - arr[i][pos - 1];
- int used = min<int>(seg, abs(t - sum));
- best[i] -= used;
- sum -= used;
- cnt_le[i]--;
- if (used == seg) {
- int cur = get(i, best[i] - 1) - get(i, best[i]);
- Note note(cur, cnt_le[i] - 1, i);
- st.insert(note);
- }
- }
- }
- for (int val : best) {
- cout << val << ' ';
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement