Facebook
From Abdus Salam, 1 Year ago, written in C++.
Embed
Download Paste or View Raw
Hits: 97
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long int
  5. #define ull unsigned long long int
  6. #define Max 1000005
  7. #define _fastio  ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0);
  8. #define ALL(x) x.begin(),x.end()
  9. #define pi acos(-1.0)
  10. #define pb push_back
  11. #define load(array,size)  for(ll i=0 ; i<size ; i++) cin>>array[i] ;
  12. #define endl 'n'
  13.  
  14. #define sc(n) scanf("%d",&n)
  15. #define scan(n) scanf("%lld",&n)
  16. /*        speed up       */
  17. #pragma GCC target ("avx2")
  18. #pragma GCC optimization ("O3")
  19. #pragma GCC optimization ("unroll-loops")
  20. #define sci(n) scanf("%d",&n);
  21. #define scl(n) scanf("%lld",&n);
  22. #define load1(array,size)  for(ll i=1 ; i<=size ; i++) cin>>array[i] ;
  23. #define INF    1LL<<19
  24. #define pll pair<ll,ll>
  25. #define mst(arr,value) memset(arr,value,sizeof(arr));
  26. #define tst() _fastio;int testsalam;cin >> testsalam;while(testsalam--)
  27. #define Mod 100000007
  28. #define YES cout<<"Yes"<<endl;
  29. #define NO cout<<"No"<<endl;
  30. #define fi first
  31. #define se second
  32.  
  33. struct HASH{
  34. size_t operator()(const pair<int,int>&x)const{
  35. return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
  36. }
  37. };
  38.  
  39.  
  40. //void inversemod(ll n, ll mod, ll invmod[],ll MOD){for(int i=2; i<=n; ++i) invmod[i]=MOD-MOD/i*invmod[MOD%i]%MOD;}
  41. //void bigmod( ll a, ll b ,ll mod){ll r = 1;a = a%mod;while(b){if( b%2 ) r = (r*a)%mod;b = b/2;a = (a*a)%mod; }return (ll)r;}
  42. string inttostr(ll n){std::stringstream ss;ss << n;string out=ss.str();return out;}
  43. ll stringtoint(string s){istringstream iss (s);ll n;iss >> n;return n;}
  44. template <typename T> T gcd(T a,T b){if(a < 0)return gcd(-a,b);if(b < 0)return gcd(a,-b);return (b == 0)?a:gcd(b,a%b);}
  45. template <typename T> T lcm(T a,T b) {if(a < 0)return lcm(-a,b);if(b < 0)return lcm(a,-b);return a*(b/gcd(a,b));}
  46. typedef vector<ll>vec;
  47. typedef pair<ll,ll> pii;
  48. typedef map<ll,ll>mpll;
  49. typedef set<ll>seet;
  50. typedef tuple<ll,ll,ll>tpl;
  51. typedef unordered_map<pair<int,int>,int,HASH>u_map;
  52.  
  53. int main()
  54. {
  55.     cout<<"Name         : Abdus Salam"<<endl;
  56.     cout<<"Roll         : 1710035"<<endl;
  57.     cout<<"nnEnter number of process: "<<endl;
  58.     int n;
  59.     cin>> n;
  60.     int at[n+10],bt[n+10],ct[n+10],ta[n+10],wt[n+10];
  61.  
  62.     vector<pair<int,int>>inp;
  63.  
  64.     cout<<"Enter AT and BT(space separated)"<<endl;
  65.     for (int i = 0; i < n; ++i)
  66.     {
  67.         int a,b;
  68.         cin>> a>> b;
  69.         inp.push_back({a,b});
  70.     }
  71.     // sort(inp.begin(),inp.end());
  72.     int ctime=0;
  73.     int totalTAtime=0,totalWtime=0;
  74.     for (int i = 0; i < n; ++i)
  75.     {
  76.         ctime+=inp[i].second;
  77.         ct[i]=ctime;
  78.         ta[i]=ct[i]-inp[i].first;
  79.         wt[i]=ta[i]-inp[i].second;
  80.         totalTAtime+=ta[i];
  81.         totalWtime+=wt[i];
  82.     }
  83.     cout<<"PId    |AT    |BT    |CT      |  TA         |WT"<<endl;
  84.     for (int i = 0; i < n; ++i)
  85.     {
  86.         cout<<i+1<<"%t"<<inp[i].first<<"%t"<<inp[i].second<<"%t"<<ct[i]<<"%t"<<ta[i]<<"%t"<<wt[i]<<endl;
  87.     }
  88.  
  89.     cout<<"Average TurnAround time: "<<totalTAtime/n<<endl;
  90.     cout<<"Average Wating Time: "<<totalWtime/n<<endl;
  91. }
  92. /*
  93.     remember:
  94.     ll vs int
  95.     array size
  96.     case for 1 2 3
  97. */
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. #include <bits/stdc++.h>
  107. using namespace std;
  108.  
  109. #define ll long long int
  110. #define ull unsigned long long int
  111. #define Max 1000005
  112. #define _fastio  ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0);
  113. #define ALL(x) x.begin(),x.end()
  114. #define pi acos(-1.0)
  115. #define pb push_back
  116. #define load(array,size)  for(ll i=0 ; i<size ; i++) cin>>array[i] ;
  117. #define endl 'n'
  118.  
  119. #define sc(n) scanf("%d",&n)
  120. #define scan(n) scanf("%lld",&n)
  121. /*        speed up       */
  122. #pragma GCC target ("avx2")
  123. #pragma GCC optimization ("O3")
  124. #pragma GCC optimization ("unroll-loops")
  125. #define sci(n) scanf("%d",&n);
  126. #define scl(n) scanf("%lld",&n);
  127. #define load1(array,size)  for(ll i=1 ; i<=size ; i++) cin>>array[i] ;
  128. #define INF    1LL<<19
  129. #define pll pair<ll,ll>
  130. #define mst(arr,value) memset(arr,value,sizeof(arr));
  131. #define tst() _fastio;int testsalam;cin >> testsalam;while(testsalam--)
  132. #define Mod 100000007
  133. #define YES cout<<"Yes"<<endl;
  134. #define NO cout<<"No"<<endl;
  135. #define fi first
  136. #define se second
  137.  
  138. struct HASH{
  139. size_t operator()(const pair<int,int>&x)const{
  140. return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
  141. }
  142. };
  143.  
  144.  
  145. //void inversemod(ll n, ll mod, ll invmod[],ll MOD){for(int i=2; i<=n; ++i) invmod[i]=MOD-MOD/i*invmod[MOD%i]%MOD;}
  146. //void bigmod( ll a, ll b ,ll mod){ll r = 1;a = a%mod;while(b){if( b%2 ) r = (r*a)%mod;b = b/2;a = (a*a)%mod; }return (ll)r;}
  147. string inttostr(ll n){std::stringstream ss;ss << n;string out=ss.str();return out;}
  148. ll stringtoint(string s){istringstream iss (s);ll n;iss >> n;return n;}
  149. template <typename T> T gcd(T a,T b){if(a < 0)return gcd(-a,b);if(b < 0)return gcd(a,-b);return (b == 0)?a:gcd(b,a%b);}
  150. template <typename T> T lcm(T a,T b) {if(a < 0)return lcm(-a,b);if(b < 0)return lcm(a,-b);return a*(b/gcd(a,b));}
  151. typedef vector<ll>vec;
  152. typedef pair<ll,ll> pii;
  153. typedef map<ll,ll>mpll;
  154. typedef set<ll>seet;
  155. typedef tuple<ll,ll,ll>tpl;
  156. typedef unordered_map<pair<int,int>,int,HASH>u_map;
  157.  
  158. int main()
  159. {
  160.     cout<<"Name         : Abdus Salam"<<endl;
  161.     cout<<"Roll         : 1710035"<<endl;
  162.         cout<<"Enter the number of process: "<<endl;
  163.         int n;
  164.         cin>>n;
  165.         map<int,pair<int,int> >v;
  166.         int a,b,p,tBT=0;
  167.         map<int,int>CT,WT,TAT,AT;
  168.         cout<<"Enter process number,arrival time and burst time: "<<endl;
  169.         for(int i=1;i<=n;i++)
  170.         {
  171.                 cin>>p>>a>>b;
  172.                 v[p]={a,b};
  173.                 tBT+=b;
  174.                 AT[p]=a;
  175.         }
  176.  
  177.         for(int i=0;i<tBT;i++)
  178.         {
  179.                 vector<pair<int,int> >v1;
  180.                 pair<int,int>pr;
  181.                 for(auto it:v)
  182.                 {
  183.                         pr=it.second;
  184.                         if(pr.first<=i && pr.second>0)
  185.                         {
  186.                                 v1.push_back(make_pair(pr.second,it.first));
  187.                         }
  188.                 }
  189.                 if(v1.size()!=0)
  190.                 {
  191.                         sort(v1.begin(),v1.end());
  192.                         pr=v[v1[0].second];
  193.                         WT[v1[0].second]+=i-pr.first;
  194.                         a=i+1;
  195.                         b=pr.second-1;
  196.                         v[v1[0].second]={a,b};
  197.                         if(b==0)
  198.                         {
  199.                                 CT[v1[0].second]=i+1;
  200.                                 TAT[v1[0].second]=i-AT[v1[0].second]+1;
  201.                         }
  202.                 }
  203.         }
  204.  
  205.         int ct[n+10],wt[n+10],ta[n+10];
  206.         for(auto it:CT)
  207.                 ct[it.first]=it.second;
  208.         for(auto it:WT)
  209.                 wt[it.first]=it.second;
  210.         for(auto it:TAT)
  211.                 ta[it.first]=it.second;
  212.  
  213.  
  214.         printf(" nn n tProcess t AT t BT t CT t TA t WT t");
  215.     printf(" n -----------------------------------------------------");
  216.  
  217.     for (int i=1; i<=n; i++)
  218.     {
  219.         printf("  n tP%d tt |%d t |%d t |%d t |%d t |%d t",i,v[i].first,v[i].second,ct[i],ta[i],wt[i]);
  220.  
  221.     }
  222.  
  223. }
  224. /*
  225.  
  226. 4
  227. 1 0 8
  228. 2 1 4
  229. 3 2 9
  230. 4 3 5
  231.  
  232. */
  233.