999ms

https://codeforces.com/group/YnP1G037Yb/contest/102168/probl

Jan 27th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define all(x) (x).begin(),(x).end()
  3.  
  4. using namespace std;
  5. using ll = long long;
  6.  
  7. int main() {
  8.     ios_base::sync_with_stdio(false);
  9.     cin.tie(nullptr);
  10.     cout.tie(nullptr);
  11.     int n;
  12.     cin >> n;
  13.     vector<ll> arr(n);
  14.     vector<ll> brr(n);
  15.     for (int i = 0; i < n; i++) {
  16.         cin >> arr[i];
  17.     }
  18.     for (int i = 0; i < n; i++) {
  19.         cin >> brr[i];
  20.     }
  21.     if (n == 1) {
  22.         if (arr[0] >= brr[0]) {
  23.             cout << arr[0] - brr[0] << endl;
  24.             return 0;
  25.         } else {
  26.             cout << -1 << endl;
  27.             return 0;
  28.         }
  29.     }
  30.     ll dlt = n - 2;
  31.     ll sum1 = accumulate(all(arr), 0LL);
  32.     ll sum2 = accumulate(all(brr), 0LL);
  33.     if (n == 2) {
  34.         if (sum1 != sum2) {
  35.             cout << -1 << endl;
  36.             return 0;
  37.         } else {
  38.             cout << max(arr[0] - brr[0], 0LL) << ' ' << max(arr[1] - brr[1], 0LL) << endl;
  39.             return 0;
  40.         }
  41.     }
  42.     if (sum2 < sum1 || (sum2 - sum1) % dlt != 0) {
  43.         cout << -1 << endl;
  44.         return 0;
  45.     }
  46.     ll countOfTabs = (sum2 - sum1) / dlt;
  47.     for (int i = 0; i < n; i++) {
  48.         arr[i] += countOfTabs;
  49.     }
  50.     vector<ll> ans(n, 0);
  51.     for (int i = 0; i < n; i++) {
  52.         if (arr[i] > brr[i]) {
  53.             ll curDelta = arr[i] - brr[i];
  54.             if (curDelta & 1) {
  55.                 cout << -1 << endl;
  56.                 return 0;
  57.             }
  58.             countOfTabs -= (arr[i] - brr[i]) / 2;
  59.             ans[i] = (arr[i] - brr[i]) / 2 ;
  60.         } else if (arr[i] < brr[i]) {
  61.             cout << -1 << endl;
  62.             return 0;
  63.         }
  64.     }
  65.     if (countOfTabs != 0) {
  66.         cout << -1 << endl;
  67.         return 0;
  68.     } else {
  69.         for (auto& val : ans) {
  70.             cout << val << ' ';
  71.         }
  72.         cout << endl;
  73.     }
  74. }
Add Comment
Please, Sign In to add comment