Advertisement
SansPapyrus683

Robot Instructions Solution

Mar 6th, 2022
1,310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <algorithm>
  5.  
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. using std::pair;
  10. using Point = pair<long long, long long>;
  11.  
  12. Point add_pts(const Point& p1, const Point& p2) {
  13.     return {p1.first + p2.first, p1.second + p2.second};
  14. }
  15.  
  16. vector<pair<Point, int>> subset_sums(const vector<Point>& points) {
  17.     vector<pair<Point, int>> all_points{{{0, 0}, 0}};
  18.     for (const Point& p : points) {
  19.         int size = all_points.size();
  20.         for (int i = 0; i < size; i++) {
  21.             const pair<Point, int>& prev = all_points[i];
  22.             all_points.push_back({add_pts(p, prev.first), prev.second + 1});
  23.         }
  24.     }
  25.     return all_points;
  26. }
  27.  
  28. // 2022 feb gold (input ommitted due to length)
  29. int main() {
  30.     int instruction_num;
  31.     std::cin >> instruction_num;
  32.  
  33.     Point target;
  34.     std::cin >> target.first >> target.second;
  35.    
  36.     vector<Point> half1(instruction_num / 2);
  37.     vector<Point> half2(instruction_num - half1.size());
  38.     for (int p = 0; p < instruction_num; p++) {
  39.         if (p < half1.size()) {
  40.             std::cin >> half1[p].first >> half1[p].second;
  41.         } else {
  42.             int ind = p - half1.size();
  43.             std::cin >> half2[ind].first >> half2[ind].second;
  44.         }
  45.     }
  46.  
  47.     vector<pair<Point, int>> subs1 = subset_sums(half1);
  48.     vector<pair<Point, int>> subs2 = subset_sums(half2);
  49.     std::sort(subs1.begin(), subs1.end());
  50.     std::sort(subs2.begin(), subs2.end(), std::greater<pair<Point, int>>());
  51.  
  52.     auto add = [&] (int ind1, int ind2) -> Point {
  53.         return add_pts(subs1[ind1].first, subs2[ind2].first);
  54.     };
  55.  
  56.     vector<long long> valid_subsets(instruction_num + 1);
  57.     int at2 = 0;
  58.     for (int at1 = 0; at1 < subs1.size(); at1++) {
  59.         while (at2 < subs2.size() && add(at1, at2) > target) {
  60.             at2++;
  61.         }
  62.         if (add(at1, at2) != target) {
  63.             continue;
  64.         }
  65.        
  66.         Point initial1 = subs1[at1].first;
  67.         Point initial2 = subs2[at2].first;
  68.         std::map<int, long long> sizes1{{subs1[at1].second, 1}};
  69.         std::map<int, long long> sizes2{{subs2[at2].second, 1}};
  70.         while (at1 + 1 < subs1.size() && subs1[at1 + 1].first == initial1) {
  71.             at1++;
  72.             sizes1[subs1[at1].second]++;
  73.         }
  74.         while (at2 + 1 < subs2.size() && subs2[at2 + 1].first == initial2) {
  75.             at2++;
  76.             sizes2[subs2[at2].second]++;
  77.         }
  78.         for (const auto& [sz1, amt1] : sizes1) {
  79.             for (const auto& [sz2, amt2] : sizes2) {
  80.                 valid_subsets[sz1 + sz2] += amt1 * amt2;
  81.             }
  82.         }
  83.     }
  84.  
  85.     for (int i = 1; i <= instruction_num; i++) {
  86.         cout << valid_subsets[i] << endl;
  87.     }
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement