Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <vector>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- int main()
- {
- // declare variables
- int N; // N <= 100 店家編號
- int M; // M <= 100,000 M筆記錄/指令
- cin >> N >> M;
- // 使用N參數 - 建立店家List
- vector< list<int> > storeQueueVector(0);
- for (int i = 0; i < N; i++)
- {
- list<int> storeQueue(0);
- storeQueueVector.push_back(storeQueue);
- }
- // 使用M參數 - 處理各指令
- while(M)
- {
- M--;
- // declare variables
- char cmd[10];
- int storeID_now, storeID_to;
- int otakuID;
- cin >> cmd;
- // get inputs
- if (strcmp(cmd,"LEAVE") == 0)
- {
- cin >> storeID_now;
- storeID_now--; // -- to match index
- otakuID = -1;
- storeID_to = -1;
- }
- else if (strcmp(cmd,"ADD") == 0)
- {
- cin >> storeID_now >> otakuID;
- storeID_now--; // -- to match index
- storeID_to = -1;
- }
- else if (strcmp(cmd,"JOIN") == 0)
- {
- cin >> storeID_now >> storeID_to;
- storeID_now--; // -- to match index
- storeID_to--; // -- to match index
- otakuID = -1;
- }
- // calculate
- if (strcmp(cmd,"ADD") == 0)
- {
- list<int>* currStore = &storeQueueVector[storeID_now]; // 注意: 這邊要拿mem addr, 不然無法改到原本物件的值
- currStore->push_back(otakuID); // 有人加入排隊隊伍了(從最後面)
- }// end of "ADD"
- if (strcmp(cmd,"LEAVE") == 0)
- {
- list<int>* currStore = &storeQueueVector[storeID_now]; // 注意: 這邊要拿mem addr, 不然無法改到原本物件的值
- if (currStore->empty())
- {
- cout << "queue " << storeID_now + 1 << " is empty!" << "\n";
- }
- else if (!currStore->empty())
- {
- currStore->pop_front(); // 有人拿到遊戲離開隊伍了(從最前面)
- }
- }// end of "LEAVE"
- if (strcmp(cmd,"JOIN") == 0)
- {
- list<int>* currStore = &storeQueueVector[storeID_now]; // 注意: 這邊要拿mem addr, 不然無法改到原本物件的值
- list<int>* toStore = &storeQueueVector[storeID_to]; // 注意: 這邊要拿mem addr, 不然無法改到原本物件的值
- // this does not construct/delete any elements.
- // It simplely change some pointers to let all elements in currStore to be of toStore
- // and remove all elements in currStore which also just manipulates its pointers.
- // It's a very efficient way to shift elements from one to another.
- toStore->splice(toStore->end(), *currStore); // 因為currStore沒遊戲賣了,所有人照原本順序到toStore排隊了
- }// end of "JOIN"
- }// end of while(M)
- // ANS - 將每家店現在排隊狀況印出來
- vector< list<int> >::const_iterator vectorEnd = storeQueueVector.end();
- int i = 0;
- for(vector< list<int> >::const_iterator itr = storeQueueVector.begin();
- itr != vectorEnd; ++itr)
- {
- cout << "queue " << i + 1 << ":";
- i++;
- if (itr->empty())
- {
- cout << " empty" << "\n";
- continue;
- }
- // inner for
- list<int>::const_iterator listEnd = itr->end();
- for (list<int>::const_iterator itr_inner = itr->begin();
- itr_inner != listEnd; ++itr_inner)
- {
- cout << " " << *itr_inner;
- }// end of inner for
- cout << "\n";
- }// end of outer for
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement