SGU 271 双端队列deque

题目来源ACM ICPC 2004-2005, NEERC, Southern Subregional Contest

下面放出题目

271. Book Pile

time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard
output: standard



There is a pile of N books on the table. Two types of operations are performed over this pile: 
- a book is added to the top of the pile, 
- top K books are rotated. If there are less than K books on the table, the whole pile is rotated. 
First operation is denoted as ADD(S) where S is the name of the book, and the second operations is denoted as ROTATE. 
The maximum number of books is no more than 40000. All book names are non-empty sequences of no more than 3 capital Latin letters. The names of the books can be non-unique.

Input
The first line of input file contains 3 integer numbers N, M, K (0 <= N <= 40000; 0 <= M <= 100000; 0 <= K <= 40000). The following N lines are the names of the books in the pile before performing any operations. The book names are given in order from top book to bottom. Each of the following M lines contains the operation description. 

Output
Output the sequence of books names in the pile after performing all operations. First line corresponds to the top book. 

Sample test(s)

Input
 
 
2 3 2 A B ADD(C) ROTATE ADD(D) 
Output
 
 




大意就是给出初始的一堆书,然后有两种操作,一种是在往顶部加一本书,另一种是翻转顶部前k本书。

这个题可以使用splay写,但是有另一种简单的方法,因为是要翻转前k本书,我们可以用一个deque来维护前k本书,剩下的书放入一个vector或者stack中,通过一个变量维护是向deque的front加书,还是back加书来模拟翻转操作。

嗯(⊙_⊙)就是这样,然后就结束了。

下面放出代码

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <climits>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)>(b)?(b):(a))
#define rep(i,initial_n,end_n) for(int (i)=(initial_n);(i)<(end_n);i++)
#define repp(i,initial_n,end_n) for(int (i)=(initial_n);(i)<=(end_n);(i)++)
#define reep(i,initial_n,end_n) for((i)=(initial_n);(i)<(end_n);i++)
#define reepp(i,initial_n,end_n) for((i)=(initial_n);(i)<=(end_n);(i)++)
#define eps 1.0e-9
#define MAX_N 1010

using namespace std;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef long long ll;
typedef unsigned long long ull;

deque<string> q;
vector<string> ans;

int main() {
    int n, m, k, head = 0, len;
    string tmp;
    cin >> n >> m >>k;
    rep(i, 0, n) {
        cin >> tmp;
        q.push_back(tmp);
    }
    while(q.size() > k) ans.push_back(q.back()), q.pop_back();
    rep(i, 0, m) {
        cin >> tmp;
        if(tmp[0]== 'R') head = 1 - head;
        else {
            tmp.erase(0, tmp.find('(') + 1);
            tmp.erase(tmp.find(')'));
            if(head == 0) {
                q.push_front(tmp);
                if(q.size() > k) ans.push_back(q.back()), q.pop_back();
            }
            else {
                q.push_back(tmp);
                if(q.size() > k) ans.push_back(q.front()), q.pop_front();
            }
        }
    }
    len = ans.size();
    if(head == 0) while(!q.empty()) { cout << q.front(); puts(""); q.pop_front(); }
    else while(!q.empty()) { cout << q.back(); puts("");  q.pop_back(); }
    rep(i, 0, len) { cout << ans[len - 1 - i]; puts(""); }
    return 0;
}


你可能感兴趣的:(数据结构,算法,ACM,deque)