Advertisement
MirzaMdAzwad

TeamQueue.cpp

Aug 12th, 2021
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.26 KB | None | 0 0
  1. /*
  2. Author: Mirza Mohammad Azwad
  3. Islamic University of Technology(IUT)
  4. If you're looking at my code, enjoy :)
  5. */
  6. /// Header files, namespaces defined here
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. //#include <ext/pb_ds/assoc_container.hpp>
  10. //#include <ext/pb_ds/tree_policy.hpp>
  11. //using namespace __gnu_pbds;
  12. ///Custom macros used throughout the code defined here
  13. #define fastio  ios_base::sync_with_stdio(0);cin.tie(NULL);
  14. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  15. #define EL printf("\n")
  16. #define el '\n'
  17. #define OK printf("OK")
  18. #define pb push_back
  19. #define mp make_pair
  20. #define ep emplace_back
  21. #define X  first
  22. #define Y  second
  23. #define fillchar(a,x) memset(a, x, sizeof(a))
  24. #define OK printf("OK")
  25. #define pf push_front
  26. #define X  first
  27. #define Y  second
  28. #define int64 long long int
  29. #define fillchar(a,x) memset(a, x, sizeof(a))
  30. #define FOR(i,l,r) for (int i=l;i<=r;i++)
  31. #define FORD(i,r,l) for (int i=r;i>=l;i--)
  32. #define FORx(i,l,r,x) for (int i=l;i<=r;i+=x)
  33. #define FORDx(i,r,l,x) for (int i=r;i>=l;i-=x)
  34. ///Custom macro definition ends here
  35. ///Some type definitions
  36. //typedef int64_t ll; ///Use this for int64_t
  37. //typedef long long ll;
  38. //typedef pair<ll,ll> lll;
  39. //typedef pair<ll,int> lli;
  40. //typedef pair<int,int> ii;
  41. //using  int128 =   signed __int128;
  42. //using uint128 = unsigned __int128;
  43. ///Some type definitions ends here
  44. ///BIG INT START
  45. //const int base = 1e9;
  46. //typedef vector<int> BigInt;
  47. //
  48. //void Set(BigInt &a) {
  49. //    while (a.size() > 1 && a.back() == 0) a.pop_back();
  50. //}
  51. //
  52. //void Print(BigInt a) {
  53. //    Set(a);
  54. //    printf("%d", (a.size() == 0) ? 0 : a.back());
  55. //    FORD(i,a.size()-2,0) printf("%09d", a[i]); EL;
  56. //}
  57. //
  58. //
  59. //
  60. //BigInt Integer(string s) {
  61. //    BigInt ans;
  62. //    if (s[0] == '-') return ans;
  63. //    if (s.size() == 0) {ans.pb(0); return ans;}
  64. //    while (s.size()%9 != 0) s = '0'+s;
  65. //    for (int i=0;i<s.size();i+=9) {
  66. //        int v = 0;
  67. //        for (int j=i;j<i+9;j++) v = v*10+(s[j]-'0');
  68. //        ans.insert(ans.begin(),v);
  69. //    }
  70. //    Set(ans);
  71. //    return ans;
  72. //}
  73. //
  74. //BigInt Integer(char c[]) {
  75. //    string s = "";
  76. //    FOR(i,0,strlen(c)-1) s = s + c[i];
  77. //    return Integer(s);
  78. //}
  79. //
  80. //BigInt Integer(ll x) {
  81. //    string s = "";
  82. //    while (x > 0) s = char(x%10+'0') + s, x /= 10;
  83. //    return Integer(s);
  84. //}
  85. //
  86. //BigInt Integer(int x) {
  87. //    return Integer((ll) x);
  88. //}
  89. //
  90. //
  91. //
  92. //
  93. //void operator >> (istream &in, BigInt &a) {
  94. //    string s;
  95. //    getline(cin, s);
  96. //    a = Integer(s);
  97. //}
  98. //
  99. //void operator << (ostream &out, BigInt a) {
  100. //    Print(a);
  101. //}
  102. //
  103. //
  104. //
  105. //
  106. //bool operator < (BigInt a, BigInt b) {
  107. //    Set(a);
  108. //    Set(b);
  109. //    if (a.size() != b.size()) return (a.size() < b.size());
  110. //    FORD(i,a.size()-1,0)
  111. //        if (a[i] != b[i]) return (a[i] < b[i]);
  112. //    return false;
  113. //}
  114. //
  115. //bool operator > (BigInt a, BigInt b) {
  116. //    return (b < a);
  117. //}
  118. //
  119. //bool operator == (BigInt a, BigInt b) {
  120. //    return (!(a < b) && !(b < a));
  121. //}
  122. //
  123. //bool operator <= (BigInt a, BigInt b) {
  124. //    return (a < b || a == b);
  125. //}
  126. //
  127. //bool operator >= (BigInt a, BigInt b) {
  128. //    return (b < a || b == a);
  129. //}
  130. //
  131. //bool operator < (BigInt a, int b) {
  132. //    return (a < Integer(b));
  133. //}
  134. //
  135. //bool operator > (BigInt a, int b) {
  136. //    return (a > Integer(b));
  137. //}
  138. //
  139. //bool operator == (BigInt a, int b) {
  140. //    return (a == Integer(b));
  141. //}
  142. //
  143. //bool operator >= (BigInt a, int b) {
  144. //    return (a >= Integer(b));
  145. //}
  146. //
  147. //bool operator <= (BigInt a, int b) {
  148. //    return (a <= Integer(b));
  149. //}
  150. //
  151. //
  152. //
  153. //BigInt max(BigInt a, BigInt b) {
  154. //    if (a > b) return a;
  155. //    return b;
  156. //}
  157. //
  158. //BigInt min(BigInt a, BigInt b) {
  159. //    if (a < b) return a;
  160. //    return b;
  161. //}
  162. //
  163. //
  164. //
  165. //
  166. //BigInt operator + (BigInt a, BigInt b) {
  167. //    Set(a);
  168. //    Set(b);
  169. //    BigInt ans;
  170. //    int carry = 0;
  171. //    FOR(i,0,max(a.size(), b.size())-1) {
  172. //        if (i < a.size()) carry += a[i];
  173. //        if (i < b.size()) carry += b[i];
  174. //        ans.pb(carry%base);
  175. //        carry /= base;
  176. //    }
  177. //    if (carry) ans.pb(carry);
  178. //    Set(ans);
  179. //    return ans;
  180. //}
  181. //
  182. //BigInt operator + (BigInt a, int b) {
  183. //    return a + Integer(b);
  184. //}
  185. //
  186. //BigInt operator ++ (BigInt &a) { // ++a
  187. //    a = a + 1;
  188. //    return a;
  189. //}
  190. //
  191. //void operator += (BigInt &a, BigInt b) {
  192. //    a = a + b;
  193. //}
  194. //
  195. //void operator += (BigInt &a, int b) {
  196. //    a = a + b;
  197. //}
  198. //
  199. //
  200. //
  201. //
  202. //BigInt operator - (BigInt a, BigInt b) {
  203. //    Set(a);
  204. //    Set(b);
  205. //    BigInt ans;
  206. //    int carry = 0;
  207. //    FOR(i,0,a.size()-1) {
  208. //        carry += a[i] - (i < b.size() ? b[i] : 0);
  209. //        if (carry < 0) ans.pb(carry+base), carry = -1;
  210. //        else ans.pb(carry), carry = 0;
  211. //    }
  212. //    Set(ans);
  213. //    return ans;
  214. //}
  215. //
  216. //BigInt operator - (BigInt a, int b) {
  217. //    return a - Integer(b);
  218. //}
  219. //
  220. //void operator -- (BigInt &a) { // --a
  221. //    a = a - 1;
  222. //}
  223. //
  224. //void operator -= (BigInt &a, BigInt b) {
  225. //    a = a + b;
  226. //}
  227. //
  228. //void operator -= (BigInt &a, int b) {
  229. //    a = a - b;
  230. //}
  231. //
  232. //
  233. //
  234. //
  235. //BigInt operator * (BigInt a, BigInt b) {
  236. //    Set(a);
  237. //    Set(b);
  238. //    BigInt ans;
  239. //    ans.assign(a.size()+b.size(), 0);
  240. //    FOR(i,0,a.size()-1) {
  241. //        ll carry = 0ll;
  242. //        for (int j=0;j<b.size() || carry > 0;j++) {
  243. //            ll s = ans[i+j] + carry + (ll)a[i]*(j<b.size()?(ll)b[j]:0ll);
  244. //            ans[i+j] = s%base;
  245. //            carry = s/base;
  246. //        }
  247. //    }
  248. //    Set(ans);
  249. //    return ans;
  250. //}
  251. //
  252. //BigInt operator * (BigInt a, int b) {
  253. //    return a * Integer(b);
  254. //}
  255. //
  256. //void operator *= (BigInt &a, BigInt b) {
  257. //    a = a * b;
  258. //}
  259. //
  260. //void operator *= (BigInt &a, int b) {
  261. //    a = a * b;
  262. //}
  263. //
  264. //
  265. //
  266. //BigInt operator / (BigInt a, BigInt b) {
  267. //    Set(a);
  268. //    Set(b);
  269. //    if (b == Integer(0)) return Integer("-1");
  270. //    BigInt ans, cur;
  271. //    FORD(i,a.size()-1,0) {
  272. //        cur.insert(cur.begin(), a[i]);
  273. //        int x = 0, L = 0, R = base;
  274. //        while (L <= R) {
  275. //            int mid = (L+R)>>1;
  276. //            if (b*Integer(mid) > cur) {
  277. //                x = mid;
  278. //                R = mid-1;
  279. //            }
  280. //            else
  281. //                L = mid+1;
  282. //        }
  283. //        cur = cur - Integer(x-1)*b;
  284. //        ans.insert(ans.begin(),x-1);
  285. //    }
  286. //    Set(ans);
  287. //    return ans;
  288. //}
  289. //
  290. //BigInt operator / (BigInt a, int b) {
  291. //    Set(a);
  292. //    BigInt ans;
  293. //    ll cur = 0ll;
  294. //    FORD(i,a.size()-1,0) {
  295. //        cur = (cur*(ll)base + (ll)a[i]);
  296. //        ans.insert(ans.begin(),cur/b);
  297. //        cur %= b;
  298. //    }
  299. //    Set(ans);
  300. //    return ans;
  301. //}
  302. //
  303. //void operator /= (BigInt &a, BigInt b) {
  304. //    a = a / b;
  305. //}
  306. //
  307. //void operator /= (BigInt &a, int b) {
  308. //    a = a / b;
  309. //}
  310. //
  311. //
  312. //
  313. //BigInt operator % (BigInt a, BigInt b) {
  314. //    Set(a);
  315. //    Set(b);
  316. //    if (b == Integer(0)) return Integer("-1");
  317. //    BigInt ans;
  318. //    FORD(i,a.size()-1,0) {
  319. //        ans.insert(ans.begin(), a[i]);
  320. //        int x = 0, L = 0, R = base;
  321. //        while (L <= R) {
  322. //            int mid = (L+R)>>1;
  323. //            if (b*Integer(mid) > ans) {
  324. //                x = mid;
  325. //                R = mid-1;
  326. //            }
  327. //            else
  328. //                L = mid+1;
  329. //        }
  330. //        ans = ans - Integer(x-1)*b;
  331. //    }
  332. //    Set(ans);
  333. //    return ans;
  334. //}
  335. //
  336. //int operator % (BigInt a, int b) {
  337. //    Set(a);
  338. //    if (b == 0) return -1;
  339. //    int ans = 0;
  340. //    FORD(i,a.size()-1,0)
  341. //        ans = (ans*(base%b) + a[i]%b)%b;
  342. //    return ans;
  343. //}
  344. //
  345. //void operator %= (BigInt &a, BigInt b) {
  346. //    a = a % b;
  347. //}
  348. //
  349. //void operator %= (BigInt &a, int b) {
  350. //    a = a % Integer(b);
  351. //}
  352. //
  353. //BigInt gcd(BigInt a, BigInt b) {
  354. //    Set(a);
  355. //    Set(b);
  356. //    while (b > Integer(0)) {
  357. //        BigInt r = a%b;
  358. //        a = b;
  359. //        b = r;
  360. //    }
  361. //    Set(a);
  362. //    return a;
  363. //}
  364. //
  365. //BigInt lcm(BigInt a, BigInt b) {
  366. //    return (a*b/gcd(a,b));
  367. //}
  368. //
  369. //
  370. //BigInt sqrt(BigInt a) {
  371. //    BigInt x0 = a, x1 = (a+1)/2;
  372. //    while (x1 < x0) {
  373. //        x0 = x1;
  374. //        x1 = (x1+a/x1)/2;
  375. //    }
  376. //    return x0;
  377. //}
  378. //
  379. //
  380. //BigInt pow(BigInt a, BigInt b) {
  381. //    if (b == Integer(0)) return Integer(1);
  382. //    BigInt tmp = pow(a, b/2);
  383. //    if (b%2 == 0) return tmp * tmp;
  384. //    return tmp * tmp * a;
  385. //}
  386. //
  387. //
  388. //BigInt pow(BigInt a, int b) {
  389. //    return pow(a,(Integer(b)));
  390. //}
  391. //
  392. //
  393. //int log(int n, BigInt a) { // log_n(a)
  394. //    Set(a);
  395. //    int ans = 0;
  396. //    while (a > Integer(1)) {
  397. //        ans++;
  398. //        a /= n;
  399. //    }
  400. //    return ans;
  401. //}
  402. ///BIG INT END
  403.  
  404. ///INT128 definition starts here
  405. //template<class integer>
  406. //inline integer to_int(const string& s, size_t* idx = 0, int base = 10) {
  407. //  size_t n = s.size(), i = idx ? *idx : 0; bool sign = false; integer x = 0;
  408. //  if (s[i] == '-')
  409. //      ++i, sign = true;
  410. //  function<int(char)> char_to_digit = [&](char c) {
  411. //      static const int d[] = {'a'-10,'0'};
  412. //      return tolower(c)-d[isdigit(c)]; };
  413. //  while (i < n)
  414. //      x *= base, x += char_to_digit(s[i++]);
  415. //  if (idx)
  416. //      *idx = i;
  417. //  return sign ? -x : x; }
  418. //
  419. //template<class integer>
  420. //inline string to_string(integer x, int base = 10) {
  421. //  bool sign = false; string s;
  422. //  if (x < 0)
  423. //      x = -x, sign = true;
  424. //  function<char(int)> digit_to_char = [](int d) {
  425. //      static const char c[] = {'a'-10,'0'};
  426. //      return c[d < 10]+d; };
  427. //  do
  428. //      s += digit_to_char(x%base), x /= base;
  429. //  while (x > 0);
  430. //  if (sign)
  431. //      s += '-';
  432. //  reverse(s.begin(),s.end());
  433. //  return s; }
  434. //
  435. //template<class integer>
  436. //inline istream& read(istream& is, integer& x) {
  437. //  string s; is >> s, x = to_int<integer>(s); return is; }
  438. //
  439. //template<class integer>
  440. //inline ostream& write(ostream& os, integer x) { return os << to_string(x); }
  441. //
  442. //using  int128 =   signed __int128;
  443. //using uint128 = unsigned __int128;
  444. //
  445. //inline istream& operator>>(istream& is,  int128 &x) { return  read(is,x); }
  446. //inline istream& operator>>(istream& is, uint128 &x) { return  read(is,x); }
  447. //inline ostream& operator<<(ostream& os,  int128  x) { return write(os,x); }
  448. //inline ostream& operator<<(ostream& os, uint128  x) { return write(os,x); }
  449. ///INT128 definition ends
  450. ///Another definition for int128
  451. /*
  452. __int128 read()
  453. {
  454.     __int128 x = 0, f = 1;
  455.     char ch = getchar();
  456.     while (ch < '0' || ch > '9')
  457.     {
  458.         if (ch == '-') f = -1;
  459.         ch = getchar();
  460.     }
  461.     while (ch >= '0' && ch <= '9')
  462.     {
  463.         x = x * 10 + ch - '0';
  464.         ch = getchar();
  465.     }
  466.     return x * f;
  467. }
  468.  
  469. void print(__int128 x)
  470. {
  471.     if (x < 0)
  472.     {
  473.         putchar('-');
  474.         x = -x;
  475.     }
  476.     if (x > 9) print(x / 10);
  477.     putchar(x % 10 + '0');
  478.     //printf("\n");
  479. }
  480. std::ostream& operator<<(std::ostream& os, __int128 x)
  481. {
  482.     if(x<0) return os << "-" << -x;
  483.     if(x<10) return  os << (char)(x+'0');
  484.     return os << x/10 << (char)(x%10+'0');
  485. }*/
  486. ///int128 definition ends here
  487. ///KMP algorithm starts here
  488. //vector<int> prefix_function(string s) {
  489. //    int n = (int)s.length();
  490. //    vector<int> pi(n);
  491. //    for (int i = 1; i < n; i++) {
  492. //        int j = pi[i-1];
  493. //        while (j > 0 && s[i] != s[j])
  494. //            j = pi[j-1];
  495. //        if (s[i] == s[j])
  496. //            j++;
  497. //        pi[i] = j;
  498. //    }
  499. //    return pi;
  500. //}
  501. //
  502. //int KMPSearch(string pat, string txt){
  503. //    int lenp=(int)pat.length();
  504. //    int lent=(int)txt.length();
  505. //    vector<int>lps=prefix_function(pat);
  506. //
  507. //    int i=0;
  508. //    int j=0;
  509. //    int total=0;
  510. //    while(i<lent){
  511. //        if(pat[j]==txt[i]){
  512. //            j++;
  513. //            i++;
  514. //        }
  515. //
  516. //    if(j==lenp){
  517. //        total++;
  518. //        j=lps[j-1];
  519. //    }
  520. //    else if(i<lent && pat[j]!=txt[i]){
  521. //        if(j!=0){
  522. //            j=lps[j-1];
  523. //        }
  524. //        else i++;
  525. //    }
  526. //}
  527. //return total;
  528. //}
  529. ///KMP algorithm ends here
  530. ///Some custom function definitions
  531. //vector<long long int>vec(int n){
  532. //    vector<long long int>v(n);
  533. //    for(int i=0;i<n;i++){
  534. //        cin>>v[i];
  535. //    }
  536. //    return v;
  537. //}
  538. //deque<int>deq(deque<int>v,int n){
  539. //    int x;
  540. //    for(int i=0;i<n;i++){
  541. //        cin>>x;
  542. //        v.push_back(x);
  543. //    }
  544. //    return v;
  545. //}
  546. //void a(int (&arr)[], int n){
  547. ////    arr[n]=*arr;
  548. //    for(int i=0;i<n;i++){
  549. //        cin>>arr[i];
  550. //    }
  551. //}
  552. //void printArray(int (arr)[],int n){
  553. //    for(int i=0;i<n;i++){
  554. //        cout<< arr[i]<<" ";
  555. //    }
  556. //    cout<<endl;
  557. //}
  558. //void printVec(vector<int>v){
  559. //    for(auto u:v){
  560. //        cout<<u<<" ";
  561. //    }
  562. //    cout<<endl;
  563. //}
  564. //void printDeq(deque<int>v){
  565. //    for(auto u:v){
  566. //        cout<<u<<" ";
  567. //    }
  568. //    cout<<endl;
  569. //}
  570. ///custom function definition ends here
  571. bool cmp(pair<int,int>p,pair<int,int>q){
  572.     return p.first>=q.second;
  573. }
  574. int main() {///main function starts here
  575.     fastio
  576.     int t;
  577.     int x;
  578.     int cnt;
  579.  
  580.     int y;
  581.     int k=0;
  582.     while(cin>>t,t!=0){
  583.         unordered_map<int,int>mp1;
  584.         cnt=0;
  585.         cout<<"Scenario #"<<++k<<el;
  586.         for(int i=0;i<t;i++){
  587.             cin>>x;
  588.             for(int j=0;j<x;j++){
  589.                 cin>>y;
  590.                 mp1[y]=cnt;
  591.                 }
  592.             cnt++;
  593.             }
  594.             unordered_map<int,queue<int>>mp2;
  595.             string z;
  596.             int tmp=cnt;
  597.             cnt=0;
  598.             while(cin>>z,z!="STOP"){
  599.                 if(z=="ENQUEUE"){
  600.                     cin>>y;
  601.                     if(mp1.find(y)!=mp1.end()){
  602.                         mp2[mp1[y]].push(y);
  603.                     }
  604.                     else{
  605.                         mp2[tmp].push(y);
  606.                     }
  607.                 }
  608.                 else if(z=="DEQUEUE"){
  609.                     if(!mp2[cnt].empty()){
  610.                         cout<<mp2[cnt].front()<<el;
  611.                         mp2[cnt].pop();
  612.                     }
  613.                     else{
  614.                         cnt++;
  615.                         if(!mp2[cnt].empty()){
  616.                             cout<<mp2[cnt].front()<<el;
  617.                             mp2[cnt].pop();
  618.                         }
  619.                     }
  620.                 }
  621.             }
  622.             cout<<el;
  623.     }
  624.  
  625.     return 0;
  626. }///main function ends here
  627.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement