Advertisement
erek1e

POI Task Termites [A]

Jul 9th, 2023
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cassert>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  10.     int n; cin >> n;
  11.     vector<long long> a(n);
  12.     long long sum = 0;
  13.     int nonZero = 0;
  14.     for (long long &x : a) cin >> x, sum += x, nonZero += !!x;
  15.     vector<vector<long long>> components;
  16.     for (int i = 0; i < n; ++i) {
  17.         if (a[i]) {
  18.             if (!i || !a[i-1]) components.emplace_back();
  19.             components.back().push_back(a[i]);
  20.         }
  21.     }
  22.  
  23.     for (vector<long long> &c : components) {
  24.         vector<long long> v;
  25.         vector<long long> newbies;
  26.         for (int i = (int)c.size()-1; i >= 0; --i) newbies.push_back(c[i]);
  27.         while (!newbies.empty()) {
  28.             long long X = newbies.back();
  29.             newbies.pop_back();
  30.             v.push_back(X);
  31.             size_t m = v.size();
  32.             if (m >= 3) {
  33.                 long long x = v[m-3], M = v[m-2], y = v[m-1];
  34.                 if (x <= M && M >= y) {
  35.                     v.resize(m-3);
  36.                     newbies.push_back(x+y-M);
  37.                 }
  38.             }
  39.         }
  40.         // now the component will be decreasing and then increasing
  41.         c = v;
  42.     }
  43.  
  44.     long long difference = 0; // last player's score - other player's score
  45.     if (a.front()) {
  46.         vector<long long> &c = components.front();
  47.         reverse(c.begin(), c.end());
  48.         while (c.size() >= 2) {
  49.             size_t m = c.size();
  50.             long long x = c[m-2], y = c[m-1];
  51.             if (x >= y) break;
  52.             difference += y-x; // for the last player
  53.             c.resize(m-2);
  54.         }
  55.     }
  56.     if (a.back()) {
  57.         vector<long long> &c = components.back();
  58.         while (c.size() >= 2) {
  59.             size_t m = c.size();
  60.             long long x = c[m-2], y = c[m-1];
  61.             if (x >= y) break;
  62.             difference += y-x; // for the last player
  63.             c.resize(m-2);
  64.         }
  65.     }
  66.     vector<long long> values;
  67.     for (const vector<long long> &c : components) values.insert(values.end(), c.begin(), c.end());
  68.     sort(values.begin(), values.end());
  69.     for (size_t i = 0; i < values.size(); ++i) difference += ((i&1) ? -1 : +1) * values[i];
  70.  
  71.     if (nonZero % 2 == 0) difference = -difference;
  72.     // difference is now first score - second score
  73.     assert((sum+difference)%2 == 0);
  74.     cout << (sum+difference)/2 << ' ' << (sum-difference)/2 << endl;
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement