A stack of books, 2 operations, one operation is to add a book on the heap, the second one will be the first K-book flip
Look at others using splay tree, but you can use the double-ended queue simulation, because the book after the K book position has been determined, so only need to record in the queue header plus book or the end of the book
#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)); else Q1.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); else Q1.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 (double-ended queue emulation)