Advertisement
i_love_rao_khushboo

Egg Dropping Algorithm

Oct 2nd, 2022
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | None | 0 0
  1. // TABULATION IMPLEMENTATION
  2. // Ref: https://www.youtube.com/watch?v=UvksR0hR9nA&list=PL-Jc9J83PIiEZvXCn-c5UIBvfT8dA-8EG&index=43
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6.  
  7. #define ll long long
  8. #define ld long double
  9. #define ull unsigned long long
  10. #define pb push_back
  11. #define ppb pop_back
  12. #define mp make_pair
  13. #define F first
  14. #define S second
  15. #define PI 3.1415926535897932384626
  16. #define sz(x) ((int)(x).size())
  17.  
  18. typedef pair<int, int> pii;
  19. typedef pair<ll, ll> pll;
  20. typedef vector<int> vi;
  21. typedef vector<ll> vll;
  22. typedef vector<ull> vull;
  23. typedef vector<bool> vb;
  24. typedef vector<char> vc;
  25. typedef vector<pii> vpii;
  26. typedef vector<pll> vpll;
  27. typedef vector<vi> vvi;
  28. typedef vector<vll> vvll;
  29. typedef vector<vull> vvull;
  30. typedef vector<vb> vvb;
  31. typedef vector<vc> vvc;
  32.  
  33. /************************************************** DEBUGGER *******************************************************************************************************/
  34.  
  35. #ifndef ONLINE_JUDGE
  36. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  37. #else
  38. #define debug(x)
  39. #endif
  40.  
  41. void _print(ll t) { cerr << t; }
  42. void _print(int t) { cerr << t; }
  43. void _print(string t) { cerr << t; }
  44. void _print(char t) { cerr << t; }
  45. void _print(ld t) { cerr << t; }
  46. void _print(double t) { cerr << t; }
  47. void _print(ull t) { cerr << t; }
  48.  
  49. template <class T, class V> void _print(pair <T, V> p);
  50. template <class T> void _print(vector <T> v);
  51. template <class T> void _print(vector <vector<T>> v);
  52. template <class T> void _print(set <T> v);
  53. template <class T, class V> void _print(map <T, V> v);
  54. template <class T> void _print(multiset <T> v);
  55. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  56. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  57. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  58. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  59. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  60. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  61.  
  62. /*******************************************************************************************************************************************************************/
  63.  
  64. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  65. int rng(int lim) {
  66.     uniform_int_distribution<int> uid(0,lim-1);
  67.     return uid(rang);
  68. }
  69.  
  70. const int INF = 0x3f3f3f3f;
  71. const int mod = 1e9+7;
  72.  
  73. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  74.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  75.                          
  76. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  77. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  78.  
  79. /******************************************************************************************************************************/
  80.  
  81. int egg_drop(int floors, int eggs) {
  82.     vvi dp(eggs + 1, vi(floors + 1));
  83.  
  84.     // initialisation of dp matrix
  85.    
  86.     // if eggs = 0, then no answer
  87.     // if eggs = 1, then ans = #floors
  88.     for(int j = 0; j <= floors; j++) dp[0][j] = -1, dp[1][j] = j;
  89.    
  90.     // if floors = 0, then ans = 0
  91.     // if floors = 1, then ans = 1
  92.     for(int i = 2; i <= eggs; i++) dp[i][0] = 0, dp[i][1] = 1;
  93.    
  94.     for(int i = 2; i <= eggs; i++) {
  95.         for(int j = 2; j <= floors; j++) {
  96.             int res = INT_MAX;
  97.  
  98.             for(int rj = j - 1, lj = 0; rj >= 0 and lj < j; rj--, lj++) {
  99.                 int tmp = 1 + max(dp[i][rj], dp[i-1][lj]);
  100.                 res = min(res, tmp);
  101.             }
  102.            
  103.             dp[i][j] = res;
  104.         }
  105.     }
  106.    
  107.     return dp[eggs][floors];
  108. }
  109.  
  110. void solve()
  111. {
  112.     int floors, eggs;
  113.     cin >> floors >> eggs;
  114.    
  115.     if(eggs == 0 and floors != 0) cout << -1 << "\n";
  116.     else cout << egg_drop(floors, eggs) << "\n";
  117. }
  118.  
  119. int main()
  120. {
  121.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  122.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  123.  
  124.     // #ifndef ONLINE_JUDGE
  125.     //     freopen("input.txt", "r", stdin);
  126.     //     freopen("output.txt", "w", stdout);
  127.     // #endif
  128.    
  129.     // #ifndef ONLINE_JUDGE
  130.     //      freopen("error.txt", "w", stderr);
  131.     // #endif
  132.    
  133.     int t = 1;
  134.     // int test = 1;
  135.     // cin >> t;
  136.     while(t--) {
  137.         // cout << "Case #" << test++ << ": ";
  138.         solve();
  139.     }
  140.  
  141.     return 0;
  142. }
  143.  
  144. /* NOTE: The 3rd for() loop in the function egg_drop() can also be used in this manner:--->
  145.  
  146.    for(int k = 1; k <= j; k++) {
  147.        int tmp = 1 + max(dp[i - 1][k - 1], dp[i][j - k]);
  148.        res = min(res, tmp);
  149.    }
  150. */
  151.  
  152.  
  153. // dp[][] is a 2 D global matrix/vector of vectors, with size (eggs + 1) x (floors + 1)
  154.  
  155. // Time Complexity: O(floors^2 x eggs)
  156. // Auxiliary Space : O(floors x eggs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement