SGU 271 双端队列deque

13 篇文章 0 订阅
12 篇文章 0 订阅

题目来源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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值