Advertisement
999ms

Untitled

Sep 11th, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define all(x) begin(x),end(x)
  3. #define make_unique(x) sort(all(x)); x.erase(unique(all(x)), end(x))
  4.  
  5. using namespace std;
  6. using ll = long long;
  7.  
  8. struct Note {
  9. int dlt;
  10. int pos;
  11. int index;
  12. explicit Note(int d, int p, int i) : dlt(d), pos(p), index(i) {}
  13. bool operator < (const Note& o) const {
  14. return dlt < o.dlt || dlt == o.dlt && index < o.index;
  15. }
  16. bool operator == (const Note& o) const {
  17. return index == o.index;
  18. }
  19. bool operator != (const Note& o) const {
  20. return index != o.index;
  21. }
  22. };
  23.  
  24. int main() {
  25. ios_base::sync_with_stdio(false);
  26. cin.tie(nullptr);
  27. cout.tie(nullptr);
  28. int n;
  29. cin >> n;
  30. vector<vector<int>> arr(n);
  31. vector<vector<int>> pref(n);
  32. for (int i = 0; i < n; i++) {
  33. int m;
  34. cin >> m;
  35. arr[i].resize(m);
  36. pref[i].resize(m);
  37. for (int& val : arr[i]) cin >> val;
  38. sort(all(arr[i]));
  39. pref[i] = arr[i];
  40. for (int j = 1; j < m; j++) {
  41. pref[i][j] += pref[i][j - 1];
  42. }
  43. }
  44.  
  45. auto get = [&] (int i, int val) {
  46. int pos = lower_bound(all(arr[i]), val) - begin(arr[i]);
  47. int m = arr[i].size();
  48. if (pos == m) {
  49. return m * 1ll * val - pref[i].back();
  50. } else {
  51. ll ans = 0;
  52. if (pos) {
  53. ans += pos * 1ll * val - pref[i][pos - 1];
  54. }
  55. ans += pref[i].back() - (pos ? pref[i][pos - 1] : 0) - val * 1ll * (m - pos);
  56. return ans;
  57. }
  58. };
  59.  
  60. int t;
  61. cin >> t;
  62. vector<int> best(n);
  63. vector<int> cnt_le(n);
  64. vector<int> cnt_h(n);
  65. ll cur_answer = 0;
  66. for (int i = 0; i < n; i++) {
  67. ll cur = 1e18;
  68. for (int j = 0; j < arr[i].size(); j++) {
  69. ll nxt = get(i, arr[i][j]);
  70. if (cur >= nxt) {
  71. cur = nxt;
  72. best[i] = arr[i][j];
  73. cnt_le[i] = i + 1;
  74. cnt_h[i] = arr[i].size() - 1 - i;
  75. }
  76. }
  77. cur_answer += cur;
  78. }
  79. int sum = accumulate(all(best), 0);
  80. if (sum <= t) {
  81. set<Note> st;
  82. for (int i = 0; i < n; i++) {
  83. int cur = get(i, best[i] + 1) - get(i, best[i]);
  84. Note note{cur, cnt_le[i] - 1, i};
  85. st.insert(note);
  86. }
  87. while (sum < t) {
  88. auto note = *st.begin();
  89. st.erase(st.begin());
  90. int i = note.index;
  91. int pos = note.pos;
  92. int dlt = note.dlt;
  93. if (pos + 1 == arr[i].size()) {
  94. best[i] += (t - sum);
  95. sum = t;
  96. } else {
  97. int seg = arr[i][pos + 1] - arr[i][pos];
  98. int used = min<int>(seg, t - sum);
  99. best[i] += used;
  100. sum += used;
  101. cnt_le[i]++;
  102. if (used == seg) {
  103. int cur = get(i, best[i] + 1) - get(i, best[i]);
  104. Note note{cur, cnt_le[i] - 1, i};
  105. st.insert(note);
  106. }
  107. }
  108. }
  109. for (int val : best) {
  110. cout << val << ' ';
  111. }
  112. cout << endl;
  113.  
  114. } else {
  115. vector<int> best(n);
  116. vector<int> cnt_le(n);
  117. vector<int> cnt_h(n);
  118. ll cur_answer = 0;
  119. for (int i = 0; i < n; i++) {
  120. ll cur = 1e18;
  121. for (int j = 0; j < arr[i].size(); j++) {
  122. ll nxt = get(i, arr[i][j]);
  123. if (cur > nxt) {
  124. cur = nxt;
  125. best[i] = arr[i][j];
  126. cnt_le[i] = i + 1;
  127. cnt_h[i] = arr[i].size() - 1 - i;
  128. }
  129. }
  130. cur_answer += cur;
  131. }
  132.  
  133. set<Note> st;
  134. for (int i = 0; i < n; i++) {
  135. int cur = get(i, best[i] - 1) - get(i, best[i]);
  136. Note note{cur, cnt_le[i] - 1, i};
  137. st.insert(note);
  138. }
  139.  
  140. while (sum > t) {
  141. auto note = *st.begin();
  142. st.erase(st.begin());
  143. int i = note.index;
  144. int pos = note.pos;
  145. int dlt = note.dlt;
  146. if (pos == 0) {
  147. int d = min(best[i], sum - t);
  148. best[i] -= d;
  149. sum -= d;
  150. } else {
  151. int seg = arr[i][pos] - arr[i][pos - 1];
  152. int used = min<int>(seg, abs(t - sum));
  153. best[i] -= used;
  154. sum -= used;
  155. cnt_le[i]--;
  156. if (used == seg) {
  157. int cur = get(i, best[i] - 1) - get(i, best[i]);
  158. Note note(cur, cnt_le[i] - 1, i);
  159. st.insert(note);
  160. }
  161. }
  162. }
  163. for (int val : best) {
  164. cout << val << ' ';
  165. }
  166. cout << endl;
  167. }
  168. }
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement