Advertisement
tiom4eg

С / Forest

Dec 19th, 2021 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // tiom4eg's precompiler options
  3. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  4. // IO settings
  5. #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
  6. // Quick types
  7. #define ll long long
  8. #define ld long double
  9. #define ull unsigned long long
  10. #define pii pair <int, int>
  11. #define pll pair <ll, ll>
  12. #define vi vector <int>
  13. #define mi vector <vector <int> >
  14. // Quick functions
  15. #define endl "\n"
  16. #define F first
  17. #define S second
  18. #define all(a) a.begin(), a.end()
  19. #define pb push_back
  20. #define mp make_pair
  21. #define fint(n) int n; cin >> n
  22. #define fstr(s) string s; cin >> s
  23. #define farr(a, n) int a[n]; for (int pog = 0; pog < n; ++pog) cin >> a[pog]
  24. #define min(a, b) ((a < b) ? (a) : (b))
  25. #define max(a, b) ((a > b) ? (a) : (b))
  26. // Quick fors
  27. #define FOR1(s, n) for (int i = s; i < n; ++i)
  28. #define FOR2(s, n) for (int j = s; j < n; ++j)
  29. #define FOR3(s, n) for (int k = s; k < n; ++k)
  30. #define RFOR(n, s) for (int l = n; l >= s; --l)
  31. // Pragmas
  32. #pragma GCC optimize("Ofast") // let the chaos begin!
  33. #pragma GCC optimize ("unroll-loops")
  34. #pragma target("avx,fma")
  35. #pragma comment(linker, "/stack:200000000")
  36. #pragma target("tune=native")
  37. // PBDS
  38. #include <ext/pb_ds/assoc_container.hpp>
  39. #include <ext/pb_ds/tree_policy.hpp>
  40. #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
  41. #define ook order_of_key
  42. #define fbo find_by_order
  43. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  44. using namespace std;
  45. using namespace __gnu_pbds;
  46. mt19937 rng(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
  47.  
  48. ll op(ll a, ll b) { return (a + b - 1) / b; }
  49.  
  50. int main() {
  51.     fastIO;
  52.     ll a, b, c, d, k; cin >> a >> b >> c >> d >> k;
  53.     if (a >= k) {
  54.         cout << k;
  55.         return 0;
  56.     }
  57.     if (b >= k) {
  58.         cout << a + k;
  59.         return 0;
  60.     }
  61.     if (c >= k + a) {
  62.         cout << a + a + b + k;
  63.         return 0;
  64.     }
  65.     if (d >= k + b) {
  66.         cout << a + b + b + c + k;
  67.         return 0;
  68.     }
  69.     ll step = 5e18;
  70.     if (a > c) {
  71.         ll trav = k - a; // Расстояние от края, на котором должны находиться, чтобы выйти из леса на текущей итерации
  72.         ll stp = op(trav, a - c); // Ищем минимальное количество полных итераций, чтобы оказаться на расстоянии от края, не превосходящем trav
  73.         ll pos = k - stp * (a - c); // Вместо a мы пройдём меньшее расстояние
  74.         step = min(step, stp * (a + b + c + d) + pos); // Вычисляем количество шагов
  75.         //cout << "UP: " << stp * (a + b + c + d) + k - stp * (a - c) << endl;
  76.     }
  77.     if (c > a) { // Аналогично...
  78.         ll trav = k - (c - a);
  79.         ll stp = op(trav, c - a);
  80.         ll pos = k - stp * (c - a) + a; //
  81.         step = min(step, stp * (a + b + c + d) + a + b + pos);
  82.         //cout << "DOWN: " << stp * (a + b + c + d) + a + b + pos << endl;
  83.     }
  84.     if (b > d) {
  85.         ll trav = k - b;
  86.         ll stp = op(trav, b - d);
  87.         ll pos = k - stp * (b - d);
  88.         step = min(step, stp * (a + b + c + d) + a + pos);
  89.         //cout << "RIGHT: " << stp * (a + b + c + d) + a + pos << endl;
  90.     }
  91.     if (d > b) {
  92.         ll trav = k - (d - b);
  93.         ll stp = op(trav, d - b);
  94.         ll pos = k - stp * (d - b) + b;
  95.         step = min(step, stp * (a + b + c + d) + a + b + c + pos);
  96.         //cout << "LEFT: " << stp * (a + b + c + d) + a + b + c + pos << endl;
  97.     }
  98.     cout << step << endl; // Выводим наименьшее из возможных
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement