For each number, you must go to the stack once and out of the stack. We set the stack to state ' 1 ' and the stack is set to state ' 0 '. All States of the n number correspond to a 2n binary number consisting of n 1 and N 0. Because the operands waiting for the stack are arranged in the order of 1‥n, the number of operands in the stack is greater than or equal to the operand a (a≤b) of the stack, so the total number of output sequences = the number of binary numbers of 2n bits consisting of n 1 and N 0, and 1 of the cumulative number of the cumulative number not less than 0. In the 2n bit binary number is filled with N 1 scheme number is C (2n,n), do not fill 1 of the remaining n bits are automatically filled 0. The number of scenarios from which non-conforming requirements are subtracted (from left to right scanning, 0 cumulative number greater than 1) is sought. The characteristics of the non-conforming number are scanned by left and right, the cumulative number of m+1 0 and the cumulative number of M 1 must first appear on an odd digit 2m+1 bit, and thereafter 2 (N-M) 1 bits have n-m 1 and n-m-1 0. If the back of this 2 (n-m)-1 bits on the 0 and 1 interchange, so that it becomes n-m 0 and N-m-1 1, the result is 1 by the n+1 0 and N-1 1 is composed of 2n digits, that is, an undesirable number corresponds to a n+1 0 and N-1 1 is composed of an arrangement. In turn, any one consists of n+1 0 and n-1 1 of the binary number of 2n, because 0 of the number of 2, 2n is even, it must be on an odd bit of a 0 cumulative number of more than 1 cumulative number. Also in the following sections 0 and 1 are interchangeable, so that it is made up of n 0 and N 1 composed of 2n digits, that is, n+1 0 and n-1 1 composed of 2n digits must correspond to a non-conforming number. Thus the non-required 2n digits correspond to the permutation one by one composed of n+1 0,n-1 1. Clearly, the number of non-conforming schemes is C (2n,n+1). This results in the total number of output sequences =c (2n,n)-C (2n,n+1) =c (2n,n)/(n+1) =h (n+1).
The simulation process is as follows, DFS to populate the stack and the flag out of the stack
#include <iostream>#include<cstring>#include<cstdio>#include<stack>#defineN 100using namespacestd;intA[n];intp[2*N];intpre[2*N];intused[2*N];intindex[2*n];//if a[i]==1, then the number of index[i] is in the stackintN;intCnt//record how many out-of-stack sequencesintCintN) { intans=1; intm =2*N; intnn =N; for(intI=1; i<=n; ++i) {ans*= m--; Ans/=i; } returnans/(nn+1);}voidsolve () {memset (PRE,0,sizeof(pre)); memset (Used,0,sizeof(used)); intPreK =0; for(intI=1; i<=2*n; ++i) { if(A[i] = =1) {Pre[i]=PreK; PreK=i; } Else { intII =PreK; while(Used[ii]) II=Pre[ii]; cout<<p[index[ii]]<<" ";//out of the stackUSED[II] =1; Pre[prek]=Pre[ii]; }} cout<<Endl;}voidsolve1 () {stack<int>s; intk=1; for(intI=1; i<=2*n; ++i) { if(A[i] = =1) S.push (p[k++]); Else{cout<<s.top () <<" "; S.pop (); }} cout<<Endl;}voidDfsintKintCnt0,intcnt1) { if(k>2*N) { //solve ();solve1 (); ++CNT; return ; } if(cnt1<n) {//into the stackA[K] =1; INDEX[K]= cnt1+1;//number of steps into the stackDFS (k +1, Cnt0, cnt1+1); } if(CNT0<CNT1) {//out of the stackA[K] =0; DFS (k+1, cnt0+1, Cnt1); }}intMain () {CIN>>N; for(intI=1; i<=n; ++i) Cin>>P[i]; DFS (1,0,0); cout<<endl<<"number of simulations:"<<cnt<<Endl; cout<<"number of formulas:"<<c (n) <<Endl; return 0;}
Similar questions buy changeA 2n person lined up to enter the theater. Admission fee is 5 yuan. of which only n individuals have a 5-dollar bill, the other N people only 10 yuan banknotes, the theater no other banknotes, asked how many of the methods so long as there are 10 of people buy tickets, the ticket office has 5 yuan of money change? (will hold 5 yuan to reach the view as 5 yuan into the stack, holding 10 yuan to see as a stack of some 5 yuan out of the stack) The final result: C (2n,n)-C (2n,n+1)
n the number of sequentially into the stack, the number of stack order?