Advertisement
i_love_rao_khushboo

Untitled

Sep 12th, 2022
945
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | None | 0 0
  1. // METHOD - 2
  2.  
  3. /* CAUTION: The initialisation part of this algorithm is slightly different from others,
  4.                                  V+1 ---->
  5.             -------------------------------------------------------
  6.             |             <------∞(infinite)----->                 |
  7.             |------------------------------------------------------|   
  8.             |    |  <------∞ or (j/coin[0] iff j%coin[0]==0---->   |
  9.       (n+1) |  ^ |-------------------------------------------------|
  10.         |   |  | |                                                 |
  11.         |   |  | |                                                 |
  12.         ˅   |  0 |                                                 |
  13.             |  | |                                                 |
  14.             |  | |                                                 |
  15.             |  ˅ |                                                 |
  16.             |    |                                                 |
  17.             |    |                                                 |
  18.             --------------------------------------------------------
  19. */
  20.  
  21. #include<bits/stdc++.h>
  22. using namespace std;
  23.  
  24. #define ll long long
  25. #define ld long double
  26. #define ull unsigned long long
  27. #define pb push_back
  28. #define ppb pop_back
  29. #define mp make_pair
  30. #define F first
  31. #define S second
  32. #define PI 3.1415926535897932384626
  33. #define sz(x) ((int)(x).size())
  34.  
  35. typedef pair<int, int> pii;
  36. typedef pair<ll, ll> pll;
  37. typedef vector<int> vi;
  38. typedef vector<ll> vll;
  39. typedef vector<ull> vull;
  40. typedef vector<bool> vb;
  41. typedef vector<char> vc;
  42. typedef vector<pii> vpii;
  43. typedef vector<pll> vpll;
  44. typedef vector<vi> vvi;
  45. typedef vector<vll> vvll;
  46. typedef vector<vull> vvull;
  47. typedef vector<vb> vvb;
  48. typedef vector<vc> vvc;
  49.  
  50. /************************************************** DEBUGGER *******************************************************************************************************/
  51.  
  52. #ifndef ONLINE_JUDGE
  53. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  54. #else
  55. #define debug(x)
  56. #endif
  57.  
  58. void _print(ll t) { cerr << t; }
  59. void _print(int t) { cerr << t; }
  60. void _print(string t) { cerr << t; }
  61. void _print(char t) { cerr << t; }
  62. void _print(ld t) { cerr << t; }
  63. void _print(double t) { cerr << t; }
  64. void _print(ull t) { cerr << t; }
  65.  
  66. template <class T, class V> void _print(pair <T, V> p);
  67. template <class T> void _print(vector <T> v);
  68. template <class T> void _print(vector <vector<T>> v);
  69. template <class T> void _print(set <T> v);
  70. template <class T, class V> void _print(map <T, V> v);
  71. template <class T> void _print(multiset <T> v);
  72. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  73. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  74. 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; } }
  75. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  76. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  77. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  78.  
  79. /*******************************************************************************************************************************************************************/
  80.  
  81. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  82. int rng(int lim) {
  83.     uniform_int_distribution<int> uid(0,lim-1);
  84.     return uid(rang);
  85. }
  86.  
  87. const int INF = 0x3f3f3f3f;
  88. const int mod = 1e9+7;
  89.  
  90. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  91.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  92.                          
  93. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  94. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  95.  
  96. /******************************************************************************************************************************/
  97.  
  98. vvi dp;
  99.  
  100. int min_coins(vi &v, int n, int sum) {
  101.     // initialisation of dp matrix
  102.    
  103.     // 0 coins required for sum == 0
  104.     for(int i = 1; i <= n; i++) dp[i][0] = 0;
  105.    
  106.     // here INT_MAX-1 represents ∞, 1 is subtracted so as to avoid                                                  
  107.     // overflow when 1 will be added to INT_MAX - 1    
  108.     for(int j = 0; j <= sum; j++) dp[0][j] = INT_MAX - 1;
  109.    
  110.     // slightly different part of initialization
  111.     for(int j = 1; j <= sum; j++) {
  112.         if(j % v[0] == 0) dp[1][j] = j / v[0];
  113.         else dp[1][j] = INT_MAX - 1;
  114.     }
  115.    
  116.     // choice diagram code iterative version
  117.     for(int i = 2; i <= n; i++) {
  118.         for(int j = 1; j <= sum; j++) {
  119.             if(v[i-1] <= j) dp[i][j] = min(1 + dp[i][j - v[i-1]], dp[i-1][j]);
  120.             else dp[i][j] = dp[i-1][j];
  121.         }
  122.     }
  123.    
  124.     if(dp[n][sum] == INT_MAX - 1) return -1;
  125.     return dp[n][sum];
  126. }
  127.  
  128. void solve()
  129. {
  130.     int n, sum; cin >> n >> sum;
  131.     vi v(n);
  132.     for(int i = 0; i < n; i++) cin >> v[i];
  133.    
  134.     dp.resize(n+1);
  135.     for(int i = 0; i <= n; i++) dp[i].resize(sum+1);
  136.    
  137.     cout << min_coins(v, n, sum) << "\n";
  138. }
  139.  
  140. int main()
  141. {
  142.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  143.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  144.  
  145.     // #ifndef ONLINE_JUDGE
  146.     //     freopen("input.txt", "r", stdin);
  147.     //     freopen("output.txt", "w", stdout);
  148.     // #endif
  149.    
  150.     // #ifndef ONLINE_JUDGE
  151.     //      freopen("error.txt", "w", stderr);
  152.     // #endif
  153.    
  154.     int t = 1;
  155.     // int test = 1;
  156.     // cin >> t;
  157.     while(t--) {
  158.         // cout << "Case #" << test++ << ": ";
  159.         solve();
  160.     }
  161.  
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement