【SGU】271. Book Pile(双端队列模拟)

一摞书,2个操作,一个操作是在书堆上加一本,第二个将前K个书翻转

看别人用Splay树做的,但是可以用双端队列模拟,,因为K个书之后的书位置已经定下来了,所以只需要记录在队列头加书还是尾加书

#include<cstdio>#include<string>#include<algorithm>#include<queue>#include<stack>#include<cstring>#include<iostream>using namespace std;int main(){int N,M,K;while(scanf("%d%d%d",&N,&M,&K) != EOF){deque<string>q1;deque<string>q2;int top = 1;for(int i = 0; i < N; i++){char str[10];scanf("%s",str);if(q1.size() >= K)q2.push_back(string(str));elseq1.push_back(string(str));}for(int i = 0; i < M; i++){char str[50];scanf("%s",str);if(str[0] == 'A'){string s = "";int L = strlen(str);int ok = 0;for(int j = 0; j < L; j++){if(str[j] == '(') ok = 1;else if(str[j] == ')') ok = 0;else if(ok)s += str[j];}if(top)q1.push_front(s);elseq1.push_back(s);if(q1.size() > K){if(top){string s1 = q1.back();q1.pop_back();q2.push_front(s1);}else{string s1 = q1.front();q1.pop_front();q2.push_front(s1);}}}else top = !top;}if(top){while(!q1.empty()){cout << q1.front() << endl;q1.pop_front();}}else{while(!q1.empty()){cout << q1.back() << endl;q1.pop_back();}}while(!q2.empty()){cout << q2.front() << endl;q2.pop_front();}}return 0;}

让你的心情地落到极点,一直学习生活等各个方面都做不好,最终害的还是自己。

【SGU】271. Book Pile(双端队列模拟)

相关文章:

你感兴趣的文章:

标签云: