Advertisement
pb_jiang

lc t4

Oct 22nd, 2022
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. class Solution {
  2.     using ll = long long;
  3. public:
  4.     long long makeSimilar(vector<int>& ns, vector<int>& tg) {
  5.         vector<int> nse, nso;
  6.         vector<int> tge, tgo;
  7.         for (auto n: ns) if (n % 2) nso.push_back(n); else nse.push_back(n);
  8.         for (auto n: tg) if (n % 2) tgo.push_back(n); else tge.push_back(n);
  9.         sort(nso.begin(), nso.end());
  10.         sort(nse.begin(), nse.end());
  11.         sort(tgo.begin(), tgo.end());
  12.         sort(tge.begin(), tge.end());
  13.        
  14.         ll ret = 0;
  15.         for (int i = 0; i < nso.size(); ++i) {
  16.             if (nso[i] < tgo[i])
  17.                 ret += (tgo[i] - nso[i]) / 2;
  18.         }
  19.         for (int i = 0; i < nse.size(); ++i) {
  20.             if (nse[i] < tge[i])
  21.                 ret += (tge[i] - nse[i]) / 2;
  22.         }
  23.         return ret;
  24.     }
  25. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement