Advertisement
999ms

Untitled

Jan 23rd, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 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. ll Solve(ll x1, ll y1, ll x2, ll y2, ll n1, ll n2) {
  8.     return max((x1 + y1) * n1 - y1, (x2 + y2) * n2 - y2);
  9. }
  10.  
  11.  
  12. int main() {
  13.     ios_base::sync_with_stdio(false);
  14.     cin.tie(nullptr);
  15.     cout.tie(nullptr);
  16.     ll n;
  17.     cin >> n;
  18.     ll x1, y1, x2, y2;
  19.     cin >> x1 >> y1 >> x2 >> y2;
  20.     ll left = 0;
  21.     ll right = n;
  22.     ll mid1;
  23.     ll mid2;
  24.     ll ans = 3e18;
  25.     while (left <= right) {
  26.         mid1 = left + (right - left) / 3;
  27.         mid2 = right - (right - left) / 3;
  28.         ll first = Solve(x1, y1, x2, y2, mid1, n - mid1);
  29.         ll second = Solve(x1, y1, x2, y2, mid2, n - mid2);
  30.         if (first <= second) {
  31.             ans = min(ans, first);
  32.             right = mid2 - 1;
  33.         } else {
  34.             ans = min(ans, second);
  35.             left = mid1 + 1;
  36.         }
  37.     }
  38.     cout << ans << "\n";
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement