Advertisement
uopspop

Untitled

Feb 25th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <vector>
  4. #include <cstdio>
  5. #include <cstring>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     // declare variables
  11.     int N; // N <= 100 店家編號
  12.     int M; // M <= 100,000 M筆記錄/指令
  13.     cin >> N >> M;
  14.  
  15.     // 使用N參數 - 建立店家List
  16.     vector< list<int> > storeQueueVector(0);
  17.     for (int i = 0; i < N; i++)
  18.     {
  19.         list<int> storeQueue(0);
  20.         storeQueueVector.push_back(storeQueue);
  21.     }
  22.  
  23.     // 使用M參數 - 處理各指令
  24.     while(M)
  25.     {
  26.         M--;
  27.  
  28.         // declare variables
  29.         char cmd[10];
  30.         int storeID_now, storeID_to;
  31.         int otakuID;
  32.         cin >> cmd;
  33.  
  34.         // get inputs
  35.         if (strcmp(cmd,"LEAVE") == 0)
  36.         {
  37.             cin >> storeID_now;
  38.             storeID_now--; // -- to match index
  39.  
  40.             otakuID = -1;
  41.             storeID_to = -1;
  42.         }
  43.         else if (strcmp(cmd,"ADD") == 0)
  44.         {
  45.             cin >> storeID_now >> otakuID;
  46.             storeID_now--; // -- to match index
  47.  
  48.             storeID_to = -1;
  49.         }
  50.         else if (strcmp(cmd,"JOIN") == 0)
  51.         {
  52.             cin >> storeID_now >> storeID_to;
  53.             storeID_now--; // -- to match index
  54.             storeID_to--; // -- to match index
  55.  
  56.             otakuID = -1;
  57.         }
  58.  
  59.         // calculate
  60.         if (strcmp(cmd,"ADD") == 0)
  61.         {
  62.             list<int>* currStore = &storeQueueVector[storeID_now]; // 注意: 這邊要拿mem addr, 不然無法改到原本物件的值
  63.             currStore->push_back(otakuID); // 有人加入排隊隊伍了(從最後面)
  64.         }// end of "ADD"
  65.  
  66.         if (strcmp(cmd,"LEAVE") == 0)
  67.         {
  68.             list<int>* currStore = &storeQueueVector[storeID_now]; // 注意: 這邊要拿mem addr, 不然無法改到原本物件的值
  69.             if (currStore->empty())
  70.             {
  71.                 cout << "queue " << storeID_now + 1 << " is empty!" << "\n";
  72.             }
  73.             else if (!currStore->empty())
  74.             {
  75.                 currStore->pop_front(); // 有人拿到遊戲離開隊伍了(從最前面)
  76.             }
  77.         }// end of "LEAVE"
  78.  
  79.         if (strcmp(cmd,"JOIN") == 0)
  80.         {
  81.             list<int>* currStore = &storeQueueVector[storeID_now]; // 注意: 這邊要拿mem addr, 不然無法改到原本物件的值
  82.             list<int>* toStore = &storeQueueVector[storeID_to]; // 注意: 這邊要拿mem addr, 不然無法改到原本物件的值
  83.             // this does not construct/delete any elements.
  84.             // It simplely change some pointers to let all elements in currStore to be of toStore
  85.             // and remove all elements in currStore which also just manipulates its pointers.
  86.             // It's a very efficient way to shift elements from one to another.
  87.             toStore->splice(toStore->end(), *currStore); // 因為currStore沒遊戲賣了,所有人照原本順序到toStore排隊了
  88.         }// end of "JOIN"
  89.     }// end of while(M)
  90.  
  91.  
  92.     // ANS - 將每家店現在排隊狀況印出來
  93.     vector< list<int> >::const_iterator vectorEnd = storeQueueVector.end();
  94.     int i = 0;
  95.     for(vector< list<int> >::const_iterator itr = storeQueueVector.begin();
  96.             itr != vectorEnd; ++itr)
  97.     {
  98.         cout << "queue " << i + 1 << ":";
  99.         i++;
  100.         if (itr->empty())
  101.         {
  102.             cout << " empty" << "\n";
  103.             continue;
  104.         }
  105.         // inner for
  106.         list<int>::const_iterator listEnd = itr->end();
  107.         for (list<int>::const_iterator itr_inner = itr->begin();
  108.                 itr_inner != listEnd; ++itr_inner)
  109.         {
  110.             cout << " " << *itr_inner;
  111.         }// end of inner for
  112.         cout << "\n";
  113.     }// end of outer for
  114.     return(0);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement