Dang_Quan_10_Tin

BUYMILK HSGVinh Phuc L9 2015-2016

Jan 15th, 2022
1,008
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define task "BUYMILK"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. constexpr int N = 1e5 + 5;
  12. ll A, B, C, D;
  13.  
  14. void Read()
  15. {
  16.     cin >> A >> B >> C >> D;
  17. }
  18.  
  19. void Solve()
  20. {
  21.     if (B <= C - D) // Sữa hộp rẻ hơn sữa chai
  22.         cout << A / B;
  23.     else
  24.     {
  25.         ll l = 1, m, h = A / (C - D);
  26.  
  27.         while (l <= h)
  28.         {
  29.             m = (l + h) / 2;
  30.             if (A >= (C - D) * m && A - (m - 1) * (C - D) >= C)
  31.                 l = m + 1;
  32.             else
  33.                 h = m - 1;
  34.         }
  35.  
  36.         ll ans = h; // Mua h chai sữa
  37.         A -= h * (C - D);
  38.  
  39.         ans += A / B;
  40.  
  41.         cout << ans;
  42.     }
  43. }
  44.  
  45. int32_t main()
  46. {
  47.     ios::sync_with_stdio(0);
  48.     cin.tie(0);
  49.     cout.tie(0);
  50.     if (fopen(task ".INP", "r"))
  51.     {
  52.         freopen(task ".INP", "r", stdin);
  53.         freopen(task ".OUT", "w", stdout);
  54.     }
  55.  
  56.     Read();
  57.     Solve();
  58. }
  59.  
RAW Paste Data