Description
A troop of recruits queue training, the recruits from the beginning in order sequentially numbered, side-by-line ranks, the training rules are as follows: From the beginning of a two count, where the two check out, the remaining to the small ordinal direction, and then from the beginning of a three count, where reporting three of the row, the remaining to the small ordinal direction, Continue from scratch to two count off ... , from beginning to end in rotation from one to two, one to three count off until the remaining number of not more than three people.
Input
There are multiple test data sets, number of first action Group N, followed by n rows of recruits, the number of recruits not exceeding 5000.
Output
Total n rows, corresponding to the number of recruits entered, each line outputs the original number of the remaining recruits, with a space between the numbers.
Sample Input
22040
Sample Output
1 7 191) 19 37
The main idea is a queue, the first 12 count, report 2 of the Dequeue, the second 123 off, check out 3 of the dequeue; this repeats until the number of queues is not more than 3.
Here the use of two queue alternating, for 12 off the case, only 1 of the report will be in the other team.
For 123, it is natural to report 12 to another team.
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<Set>#include<stack>#include<map>#include<queue>#include<string>#include<algorithm>#defineLL Long Longusing namespacestd;intN;voidWork () {Queue<int> q[2]; BOOLnow =0; intnum; for(inti =1; I <= N; ++i) Q[now].push (i); for (;;) { if(Q[now].size () <=3) Break; Num=0; now= !Now ; while(!q[!Now].empty ()) {num= num%2+1; if(num! =2) Q[now].push (q[!Now].front ()); q[!Now].pop (); } if(Q[now].size () <=3) Break; Num=0; now= !Now ; while(!q[!Now].empty ()) {num= num%3+1; if(num! =3) Q[now].push (q[!Now].front ()); q[!Now].pop (); } } BOOLFlag =false; while(!Q[now].empty ()) { if(flag) printf (" "); printf ("%d", Q[now].front ()); Q[now].pop (); Flag=true; } printf ("\ n");}intMain () {//freopen ("test.in", "R", stdin); intT; scanf ("%d", &T); for(intTimes =0; Times < T; ++Times ) {scanf ("%d", &N); Work (); } return 0;}
ACM Learning process-HDU 1276 Soldier Queue training problem (queue)