PID : 29357

Books changing

1000ms
65536KB
 
This problem will be judged on WHU. Original ID: 1462
64-bit integer IO format: %lld      Java class name: Main
Font Size:  

    Chlxyd has three boxes A, B and C, which used to hold books. All the books are piled on A, and the serial number of the books is in ascending order. Now he wants to put all the books to C. Every time he gets x books from A (take all the books away if not enough) to B, Box A is turned over, and then takes y books from B (take all the books away if not enough) to C , Box B is turned over, then repeats the operation until all the books are in box C. Can you find the order of the books in C ? You can get more details from the sample test case. 

Input

    Input contains several test cases. The first line is an integer T indicates the number of test cases. For each test case, there are only one line containing three numbers separated by a blank, n (1 <= n <= 10 ^ 5), x (1 <= x<= 10 ^ 9), y (1 <= y <= 10 ^ 9), the meaning is given above.
 

Output

    For each test case, output case number first, then n numbers indicates the permutation of books in C.
 

Sample Input

1
4 2 1
 

Sample Output

Case 1: 1 4 2 3
 

Hint

Initial:                                      A: 4321    B: EMPTY   C: EMPTY 
First time:          A->B               A: 21       B: 34         C: EMPTY 
                        A RESERVE      A: 12        B: 34         C: EMPTY
                        B->C               A: 12        B: 4           C: 3
                        B RESERVE      A: 12        B: 4           C: 3
Second time:      A->B              A: EMPTY   B: 214        C: 3
                         A RESERVE               THE SAME
                         B->C               A: EMPTY   B: 14        C: 23
                         B RESERVE      A: EMPTY   B: 41        C: 23
Third time:          B->C              A: EMPTY   B: 1          C: 423
                         B RESERVE               THE SAME
Forth time:         B->C               A: EMPTY   B: EMPTY   C: 1423
 
 

Source

 
题目大意: A箱子中总共有n本书  每次从中取出x本到B箱子中,A箱子中的书倒序,然后再从B箱子中取y本到C箱子,B箱子中的书倒序。  这就是一次完整的取书过程,一直按照这个规则,直到所有的书都到达C箱子。
解题思路:  可以把A,B箱子想做两端开口的桶,每从中取出x,y本书后,桶就翻转一次,达到书反序的要求。因此可以把A,B当做一个双端队列。按照题意,一步步模拟。每本书最多被移动2次,所以总的复杂度是O(2*N)。
观摩大神代码后自己再敲了一遍。
 1 #include<stdio.h>
 2 #define N 100010
 3 int BoxA[N*2],HeadA,TailA,TopA; //将BoxA,BoxB定义为双端数组,便于解决题目的每次取出后的倒序更新
 4 int BoxB[N*2],HeadB,TailB,TopB;
 5 int BoxC[N],TopC;
 6 int n,x,y;
 7 int main()
 8 {
 9     int T;
10     while(scanf("%d",&T)!=EOF) for(int cas=1;cas<=T;cas++)
11     {
12         scanf("%d%d%d",&n,&x,&y);
13         TopA=TopB=1;           //TopA,TopB,为1时 表示从正面取,为2时从反面取
14         HeadA=TailA=N;
15         HeadB=TailB=N;
16         for(int i=0;i<n;i++)
17         {  BoxA[TailA++]=n-i;  }
18         TopC=1;
19         while(TopC<=n)
20         {
21             if(TopA==1)
22             {
23                 if(TopB==1)    for(int i=0;i<x&&HeadA<TailA;i++)
24                         {BoxB[--HeadB]=BoxA[HeadA++];}
25                 else              for(int i=0;i<x&&HeadA<TailA;i++)
26                         {BoxB[TailB++]=BoxA[HeadA++];}
27                 TopA=2;   //每当TopA等于1处理完后  将箱子翻转  令TopA=2,下次从另一端取,下面处理相同
28             }
29             else
30             {
31                 if(TopB==1)    for(int i=0;i<x&&HeadA<TailA;i++)
32                         {BoxB[--HeadB]=BoxA[--TailA];}
33                 else        for(int i=0;i<x&&HeadA<TailA;i++)
34                         {BoxB[TailB++]=BoxA[--TailA];}
35                 TopA=1;
36             }
37             if(TopB==1)
38             {
39                 for(int i=0;i<y&&HeadB<TailB;i++)
40                 {    BoxC[TopC++]=BoxB[HeadB++]; }
41                 TopB=2;
42             }
43             else
44             {
45                 for(int i=0;i<y&&HeadB<TailB;i++)
46                 {  BoxC[TopC++]=BoxB[--TailB]; }
47                 TopB=1;
48             }
49         }
50         printf("Case %d:",cas);
51         for(int i=n;i>=1;i--)
52         {  printf(" %d",BoxC[i]);  }
53         printf("\n");
54     }
55     return 0;
56 }

          

 
posted @ 2013-08-27 01:07  Snail。  阅读(237)  评论(0编辑  收藏  举报