Advertisement
rafid_shad

Banker's Algorithms Operating System

Dec 19th, 2020 (edited)
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.77 KB | None | 0 0
  1. ///              +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+              ///
  2. #include<bits/stdc++.h>
  3. ///#include <ext/pb_ds/assoc_container.hpp>
  4. ///#include <ext/pb_ds/tree_policy.hpp>
  5. using namespace std;
  6. ///using namespace __gnu_pbds;
  7. ///template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  8.  
  9. ///-----------------------------------compiler optimizer--------------------------------------------//
  10. //#pragma GCC optimize("Ofast")
  11. //#pragma GCC target("avx,avx2,fma")
  12. //#pragma GCC optimization("unroll-loops")
  13.  
  14. ///--------------------------------------data type--------------------------------------------------//
  15. typedef long long int lli;
  16. typedef unsigned long long int ulli;
  17.  
  18. ///----------------------------------------stack-----------------------------------------------------//
  19. typedef stack <int> STI;
  20. typedef stack <char> STC;
  21. typedef stack <string> STS;
  22.  
  23. ///----------------------------------------queue------------------------------------------------------//
  24. typedef queue <int> QI;
  25. typedef queue <char> QC;
  26. typedef queue <string> QS;
  27. typedef priority_queue <int> PQI;
  28.  
  29. ///---------------------------------------dequeue-----------------------------------------------------//
  30. typedef deque <int> DI;
  31. typedef deque <char> DC;
  32.  
  33. ///---------------------------------------set------------------------------------------------------//
  34. typedef set <int> SI;
  35. typedef set <string> SS;
  36. typedef set <char> SC;
  37. typedef multiset <int> MSI;
  38.  
  39. ///----------------------------------------map------------------------------------------------------//
  40. typedef map <int, int> MP;
  41. typedef map <string, int> MPSI;
  42. typedef map <char, int> MPCI;
  43.  
  44. ///----------------------------------------pair------------------------------------------------------//
  45. typedef pair <int, int> PII;
  46.  
  47. ///----------------------------------------vector------------------------------------------------------//
  48. typedef vector <int> VI;
  49. typedef vector <char> VC;
  50. typedef vector <string> VS;
  51. typedef vector <lli> VLLI;
  52.  
  53. ///----------------------------------------constant------------------------------------------//
  54.  
  55. #define MAX                 1E9 + 5
  56. #define MIN                 -1E9 - 5
  57. #define INF                 1E18 + 5
  58. #define MOD                 10000007
  59. #define py                  acos(-1.0)  /// 3.1415926535897932
  60.  
  61. ///-------------------------------------------------------------------------------//
  62. #define pp1(A)              printf("%d\n", A)
  63. #define ppl(A)              printf("%lld\n", A)
  64. #define pp2(A,B)            printf("%d %d\n", A, B)
  65. #define pp3(A,B,C)          printf("%d %d %d\n", A, B, C)
  66.  
  67. #define ss1(A)              scanf("%d", &A)
  68. #define ssl(A)              scanf("%lld", &A)
  69. #define ss2(A,B)            scanf("%d %d", &A, &B)
  70. #define ss3(A,B,C)          scanf("%d %d %d", &A, &B, &C)
  71.  
  72. ///---------------------------------------------------------------------------//
  73. #define fastIO              ios::sync_with_stdio(false), cin.tie(0)
  74. #define ddd(args...)        do { cerr << #args << "-> " ;  show(args); } while(0); cerr<< endl ;
  75. #define TS                  template < typename T >
  76. #define TP                  template < typename F, typename S >
  77. #define TM                  template<typename T1, typename... T2>
  78.  
  79. #define pf                  push_front
  80. #define pb                  push_back
  81. #define popb                pop_back
  82. #define popf                pop_front
  83.  
  84. #define ub                  upper_bound
  85. #define lb                  lower_bound
  86.  
  87. #define itr                 iterator
  88. #define ritr                reverse_iterator
  89.  
  90. #define mk                  make_pair
  91. #define ff                  first
  92. #define ss                  second
  93.  
  94. ///---------------------------------------------------------------------------//
  95. #define endl                "\n"
  96. #define gap                 " "
  97. #define END                 return 0
  98. #define line                printf( "\n")
  99. #define yes                 printf( "YES\n")
  100. #define no                  printf( "NO\n")
  101.  
  102. #define Before              printf( "Before  \n")
  103. #define After               printf( "After  \n")
  104.  
  105. #define enter1              printf( "Entered 1\n")
  106. #define enter2              printf( "Entered 2\n")
  107. #define enter3              printf( "Entered 3\n")
  108.  
  109. #define Case(k,n)           printf( "Case %d: %d\n", k, n)
  110. #define Casell(k,n)         printf( "Case %lld: %lld\n", k, n)
  111.  
  112. ///-----------------------------------------------------------------------------//
  113. #define sq(a)               (a) * (a)
  114. #define SZ(a)               (int) a.size()
  115. #define all(a)              (a).begin(), (a).end()
  116. #define rall(a)             (a).rbegin(), (a).rend()
  117.  
  118. #define mem(x, y)           memset(x, y, sizeof(x))
  119. #define unq(v)              v.erase( unique( all(v)), v.end())
  120. #define rev(v)              reverse( all(v))
  121. #define sortV(v)            sort( all(v))
  122. #define sortA(a,n)          sort(a, a+n)
  123.  
  124. #define to_upper(s)         transform( s.begin(), s.end(), s.begin(), ::toupper)
  125. #define to_lower(s)         transform( s.begin(), s.end(), s.begin(), ::tolower)
  126. #define to_int(s)           stoi(s)
  127.  
  128. ///--------------------------------------------------------------------------------//
  129. #define Erase(V,I)          (V).erase((V).begin()+I)
  130. #define Insert(V,I,M)       (V).insert((V).begin()+I,M)
  131.  
  132. #define read()              freopen("input.txt", "r", stdin)
  133. #define write()             freopen("output.txt", "w", stdout)
  134.  
  135. ///-------------------------------------------------------------------------------------------------------------------------------//
  136. #define loop(i, n)          for(int i = 0; i < n; i++)
  137. #define loops(i, x, n)      for(int i = x; i < n; i++)
  138. #define loopr(i, n)         for(int i = n-1; i >= 0; i--)
  139. #define loopt(i, n)         for(int i = 1; i <= n; i++)
  140.  
  141. #define autoo(s)            for(auto it = s.begin(); it != s.end(); it++)
  142.  
  143. #define vin(V, N)           for(int i = 0; i < N; i++){ int X; ss1(X); V.pb(X); }
  144. #define vinll(V, N)         for(int i = 0; i < N; i++){ lli X; ssl(X); V.pb(X); }
  145.  
  146. #define ain(A, N)           for(int i = 0; i < N; i++){ ss1(A[i]); }
  147. #define ainll(A, N)         for(int i = 0; i < N; i++){ ssl(A[i]); }
  148.  
  149. #define aout(A, N)          for(int i = 0; i < N; i++){ printf("%d", A[i]); if (i < N-1) printf(" "); else printf("\n"); }
  150. #define vout(v)             for(int i = 0; i < v.size(); i++) { printf("%d", v[i]); if(i < v.size() - 1) printf(" "); else printf("\n");}
  151.  
  152. ///--------------------------------------------------Debugging-----------------------------------------------------------------//
  153.  
  154. TS void show (const T& v) { cerr << v << ' ' ;}
  155. TM void show (const T1& first, const T2&... rest){ show(first); show(rest...); }
  156. TP ostream& operator << (ostream& os, const pair<F,S>& p){return os<<"("<< p.ff << ", " << p.ss <<")" ;}
  157. TS ostream& operator << (ostream& os, const vector<T>& v){os << "{"; typename vector< T >::const_iterator it;
  158.     for(it = v.begin(); it != v.end(); it++) {if( it != v.begin() )os << ", "; os<<*it; } return os<<"}";}
  159. TS ostream& operator << (ostream& os, const set<T>& v){os << "["; typename set<T>::const_iterator it;
  160.     for(it = v.begin(); it != v.end(); it++){ if(it != v.begin())os<<", ";os <<*it; } return os <<"]"; }
  161. TS ostream& operator << (ostream& os, const multiset<T>& v){os << "["; typename multiset<T>::const_iterator it;
  162.     for(it = v.begin(); it != v.end(); it++){ if(it != v.begin())os<<", ";os <<*it; } return os <<"]"; }
  163. TP ostream& operator<<(ostream& os,const map<F,S>& v){os<<"["; typename map<F,S>::const_iterator it;
  164.     for(it = v.begin(); it != v.end(); it++){if(it != v.begin())os << ", "; os << it->ff <<" = " << it->ss; } return os<<"]";}
  165.  
  166. //-------------------------------------------------------------------------------------------------------------------------------//
  167.  
  168. lli lcm(lli a, lli b)
  169. {
  170.     return a * (b / __gcd(a, b));
  171. }
  172.  
  173. ///----------------------graph moves----------------*/
  174. int dr[] = {+1, -1, +0, +0};
  175. int dc[] = {+0, +0, +1, -1};
  176.  
  177. ///----------------------kings moves-----------------*/
  178. int X[] = {+0, +0, +1, -1, -1, +1, -1, +1};
  179. int Y[] = {-1, +1, +0, +0, +1, +1, -1, -1};
  180.  
  181. ///----------------------knights moves----------------*/
  182. int KX[] = {-2, -2, -1, -1,  1,  1,  2,  2};
  183. int KY[] = {-1,  1, -2,  2, -2,  2, -1,  1};
  184.  
  185. ///-----------------------------------template------------------------------------------------//
  186. int n, m;
  187.  
  188. int a[100+5][100+5];
  189. int b[100+5][100+5];
  190. int c[100+5][100+5];
  191. int avail[100+1];
  192. VI v;
  193.  
  194. int check(){
  195.      loopt(i, n){
  196.         int cnt = 0;
  197.         loopt(j, m){
  198.             if(!c[i][0] and avail[j]>=c[i][j]){
  199.                cnt++;
  200.             }
  201.         }
  202.          if(cnt == m){
  203.             v.pb(i);
  204.             c[i][0] = 1;
  205.             loopt(k, m){
  206.                 avail[k]+=a[i][k];
  207.             }
  208.             return 1;
  209.         }
  210.    }
  211.    return 0;
  212. }
  213.  
  214. int main()
  215. {
  216.     #ifndef ONLINE_JUDGE
  217.    // read();
  218.     #endif // ONLINE_JUDGE
  219.    // fastIO;
  220.    cin >> n >> m;
  221.  
  222.     cout << "Allocation "<<endl;
  223.    loopt(i, n){
  224.         loopt(j, m){
  225.             cin >> a[i][j];
  226.         }
  227.    }
  228.    line;
  229.    cout << "Max Need " <<endl;
  230.    loopt(i, n){
  231.         loopt(j, m){
  232.             cin >> b[i][j];
  233.         }
  234.    }
  235.    line;
  236.    cout << "Need "<<endl;
  237.    loopt(i, n){
  238.         loopt(j, m){
  239.             c[i][j] = (b[i][j]-a[i][j]);
  240.             cout << c[i][j] << gap;
  241.         }
  242.         line;
  243.    }
  244.    line;
  245.    cout << "Available "<<endl;
  246.    loopt(i,m){
  247.         cin >> avail[i];
  248.    }
  249.    loopt(i, n){
  250.  
  251.         if(!check()){
  252.             cout << "Dead lock "<< endl;
  253.             END;
  254.         }
  255.         loopt(j, m){
  256.          cout << avail[j] << gap;
  257.        }
  258.        line;
  259.    }
  260.    line;
  261.    cout << "Sequence is -> ";
  262.    loop(i, v.size()){
  263.         cout << 'P' << ++v[i] << gap;
  264.    }
  265.    line;
  266.    line;
  267.  
  268. }
  269.  
  270.  
  271.  
  272.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement