Long time no log, strange is very special more near, recently participated in the programming aspect is CSDN in the College club's online programming plug. Speaking of the programming contest. A total of three questions. The title is able to CSDN college to see the club. When the game was a little tense, the third question involved a queue, a hint, a pit ... Also blame oneself usually write code use code hint, very many STL method is vaguely know, but detailed don't know, lead to third question did not do out, regret ha! Here's the code for the third question written today.
Topic Sichuan University offline programming competition The third topic: book Transfer
Topic Details:
Xiao Qiang There are 3 boxes a,b,c used to hold books, all books (n) are numbered from small to large shun The order heap is on a. Now he wants to put all the books in C. Every time he took a book from a bookshelf (not enough to take it all) to the B,a box to flip, then from B get B book (not enough to take all) to the C,b box flip.
And then repeatedly, until all the books to C, the final C inside the order of the book. See examples.
Input descriptive narrative:
The input consists of multiple sets of data, one row per group of data. 3 numbers, N (1<=n<=10^5),A (1<=a<=10^9), B (1<=b<=10^9). The meaning is described in the topic.
Ends with a file.
Output Descriptive narrative:
One row for each set of data output. First output the number of data sets, then output n number, C in the arrangement of the book .
Answer instructions:
Input Example:
4 2 1
Output example
Case 1:1 4 2 3
Hint
Initial state a:4321 B: Empty C: Empty
First time: A->b a:21 b:34 C: Empty
A Flip A:12 b:34 C: Empty
B->c a:12 B:4 C:3
B Flip A:12 b:4 c:3
Second time: a->b A: Empty b:214 C:3
A flip a: empty b:214 c:3
B->c A: Empty b:14 c:23
B Flip A: Empty b:41 c:23
Third time: b->c A: Empty b:1 c:423
B Flip A: Empty b:1 c:423
Fourth time: B->c A: Empty B: Empty c:1423
The analysis in fact at that time did not know what algorithm, direct simulation is good, but I have forgotten a lot of knowledge. About flipping a direct marker is possible.
#include <iostream> #include <queue> #include <algorithm>using namespace std;//a, B, C Three boxes with a double-ended queue to represent the deque<int> dq[3];//, respectively, represents the flag of three box flips, flag=0 means to enqueue from front. On the other hand, enqueue from back int flag[3];int main () {int n, a, B, T=1;while (CIN >> n >> a >> b) {//Initialize three double-ended queues and flip identities for T i=0;i<3;++i) {dq[i].clear (); flag[i]=0;} Initializes a double-ended queue for (int i=1;i<=n;++i) {dq[0].push_front (i);} while (!dq[0].empty () | |!dq[1].empty ()) {//Fetch A in a box to B, but be clear whether to pick a book from the front of a box or to fetch the book from the Back if (flag[0]==0) {for (int i=0;i <a&&!dq[0].empty (); ++i) {//infer the flip condition of box B if (flag[1]==0) {Dq[1].push_front (Dq[0].front ());} Else{dq[1].push_back (Dq[0].front ());} Dq[0].pop_front ();} Take the book from A. Flip A to Flag[0]=1;} else{for (int i=0;i<a&&!dq[0].empty (); ++i) {//Infer the flip case of B box if (flag[1]==0) {Dq[1].push_front (Dq[0].back ());} Else{dq[1].push_back (Dq[0].back ());} Dq[0].pop_back ();} flag[0]=0;} Take the B book in box B and put it in C. The same to figure out which side of B to fetch the book if (flag[1]==0) {for (int i=0; I<b&&!dq[1].empty (); ++i) {Dq[2].push_front (Dq[1].front ()); Dq[1].pop_fronT ();} Flag[1]=1;} else{for (int i=0; I<b&&!dq[1].empty (); ++i) {Dq[2].push_front (Dq[1].back ());d q[1].pop_back ();} flag[1]=0;}} Finally output the order of the books in the C box cout << "Case" << T << ":", while (!dq[2].empty ()) {cout << dq[2].front () << " ";d Q[2].pop_front ();} cout << Endl; t++;} return 0;}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
The third champion in Sichuan University online programming contest: Book transfer