Advertisement
Guest User

Untitled

a guest
Jan 25th, 2023
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | Source Code | 0 0
  1. /*
  2. Problem URL:
  3. https://cses.fi/problemset/task/1161
  4. */
  5.  
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9.  
  10. void setIO(string s = "") {
  11.   ios_base::sync_with_stdio(0);
  12.   cin.tie(0);
  13.   if (s != "") {
  14.     freopen((s + ".in").c_str(), "r", stdin);
  15.     freopen((s + ".out").c_str(), "w", stdout);
  16.   }
  17. }
  18.  
  19. // DEBUG INFO:
  20.  
  21. // helper for printing a pair
  22. template <typename A, typename B>
  23. ostream &operator<<(ostream &os, const pair<A, B> &p) {
  24.   return os << '(' << p.first << ", " << p.second << ')';
  25. }
  26.  
  27. // helper for printing a container
  28. template <typename T_container, typename T = typename enable_if<
  29.                                     !is_same<T_container, string>::value,
  30.                                     typename T_container::value_type>::type>
  31. ostream &operator<<(ostream &os, const T_container &v) {
  32.   os << '{';
  33.   string sep;
  34.   for (const T &x : v)
  35.     os << sep << x, sep = ", ";
  36.   return os << '}';
  37. }
  38.  
  39. // SHORTENED MACROS
  40. #define ll long long
  41. #define ull unsigned long long
  42. #define ld long double
  43. #define nl "\n"
  44. #define sza(x) ((int)x.size())
  45. #define all(a) (a).begin(), (a).end()
  46. #define MOD 1000000007
  47. #define x first
  48. #define y second
  49.  
  50. // VERY USEFUL ORDERED CONTAINER TO USE INSTEAD OF STL
  51. #include <ext/pb_ds/assoc_container.hpp>
  52. #include <ext/pb_ds/tree_policy.hpp>
  53. using namespace __gnu_pbds;
  54. #define ordered_set(x)                                                         \
  55.   tree<x, null_type, less<x>, rb_tree_tag, tree_order_statistics_node_update>
  56.  
  57. /*
  58.  
  59. Useful Links:
  60.  
  61. - the below link matches time complexity with input size
  62. https://codeforces.com/blog/entry/21344
  63.  
  64. Problems to complete:
  65. - https://cses.fi/problemset/task/1161
  66. - https://codeforces.com/problemset/problem/1472/E
  67. - http://usaco.org/index.php?page=viewproblem2&cpid=225
  68. - https://codeforces.com/contest/860/problem/D
  69. - http://www.usaco.org/index.php?page=viewproblem2&cpid=1233
  70. - http://www.usaco.org/index.php?page=viewproblem2&cpid=645
  71.  
  72. Brainstorming:
  73.  
  74. Important Info:
  75. -
  76.  
  77. Thoughts:
  78. - this problem is definitely greedily oriented:
  79.   - the optimal move is probably to divide in half as much as possible in
  80.     general (i think) because you're still using the most
  81.  
  82. - sorting stick sizes from greatest to least and initializing two vectors, we
  83.   can add the current element to the vector with a smaller sum
  84.   - from here, we can add the sum of the previous array to the current vector
  85.   - we can continue this until all elements have been split up into their own
  86.     vector
  87.   - this works: just needs optimization
  88.   - is there a way to recursively calculate the subarray's split while
  89.     calculating an array's split?
  90.     - need a recursive solution that optimizes so the total is in O(NlogN) time:
  91.       can also make the equalarr division a O(logN) task (currently O(N))
  92.  
  93. - need to modify our custom array split function to see whether it works better;
  94.   also, is this not an O(N^2) algorithm? how was it able to run in O(NlogN)
  95.   time?
  96.  
  97. */
  98. struct customArr {
  99.   ull sum = 0;
  100.   deque<ull> arr;
  101. };
  102.  
  103. int sum, N;
  104. ull total = 0;
  105.  
  106. pair<customArr, customArr> equalSums(customArr x) {
  107.   // assuming that equalSums is already greatest-to-least sorted
  108.  
  109.   cout << x.arr << " " << x.sum << nl;
  110.  
  111.   total += x.sum;
  112.   customArr a, b;
  113.   while (!x.arr.empty()) {
  114.     if (a.sum >= b.sum) {
  115.       b.sum += x.arr.front();
  116.       b.arr.push_back(x.arr.front());
  117.       x.arr.pop_front();
  118.     } else {
  119.       a.sum += x.arr.front();
  120.       a.arr.push_back(x.arr.front());
  121.       x.arr.pop_front();
  122.     }
  123.   }
  124.  
  125.   cout << a.arr << " " << a.sum << nl;
  126.   cout << b.arr << " " << b.sum << nl;
  127.  
  128.   return make_pair(a, b);
  129. }
  130.  
  131. int main(void) {
  132.  
  133.   cin >> sum >> N;
  134.  
  135.   customArr start;
  136.   for (int i = 0; i < N; i++) {
  137.     start.arr.push_back(0);
  138.     cin >> start.arr[i];
  139.     start.sum += start.arr[i];
  140.   }
  141.  
  142.   sort(all(start.arr), greater<int>());
  143.   //   cout << start.arr << nl;
  144.  
  145.   deque<customArr> allElems;
  146.   allElems.push_back(start);
  147.   while (!allElems.empty()) {
  148.  
  149.     customArr top = allElems.front();
  150.     allElems.pop_front();
  151.  
  152.     if (sza(top.arr) == 1) {
  153.       continue;
  154.     }
  155.  
  156.     pair<customArr, customArr> res = equalSums(top);
  157.  
  158.     // cout << res.first.arr << " " << res.second.arr << " " << total << nl;
  159.  
  160.     allElems.push_back(res.first);
  161.     allElems.push_back(res.second);
  162.   }
  163.  
  164.   cout << total << nl;
  165.   return 0;
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement