3990: [SDOI2015] Sort time limit:20 Sec Memory limit:128 MB
submit:902 solved:463
[Submit] [Status] [Discuss] Description
small A has a 1-2^n arrangement a[1..2^n], he wants to order a array from small to large, small a can perform operations there are N, each operation can be executed at most once, for all I (1<=i<=n), I action is to divide the sequence from left to right into 2^{ N-I+1} segment, each paragraph exactly includes the number of 2^{i-1}, and then the overall exchange of two segments. Little A would like to know how many different sequences of operations can be ordered from small to large, and small a considers that the two sequence of operations are different, when and only if the number of operations is different, or at least one operation is different ( Different types or different operating positions).
Here is an example of an operation: n=3,a[1..8]=[3,6,1,2,7,8,5,4]. First operation, performing a 3rd operation, swapping a[1..4] and a[5..8], a[1..8 after Exchange] is [7,8,5,4,3,6,1,2]. The second operation, performing a 1th operation, swapping a[3] and a[5], a[1..8 after Exchange] is [7,8,3,4,5,6,1,2]. Third operation, perform 2nd operation, Exchange a[1..2] and a[7..8], a[1..8 after Exchange] is [1,2,3,4,5,6,7,8]. Input
First line, an integer n
Second line, 2^n Integer, A[1..2^n]output
An integer representing the answer
Sample Input 3
7 8 5 6 1 2 4 3Sample Output 6
HINT
100% of the data, 1<=n<=12.
Source
Round 1 thanks to Zky for making unofficial data
[Submit] [Status] [Discuss]
Deep search, from small to large search can determine who used the operation.
Http://hzwer.com/6839.html
1#include <cstdio>2#include <algorithm>3 #defineRep (i,l,r) for (int i=l; i<=r; i++)4typedefLong Longll;5 using namespacestd;6 7 Const intn=5010;8 intn,bin[ -],a[n];9ll fac[ -],ans;Ten One BOOLCheckintXintK) {Rep (i,x+1, x+k-1)if(a[i]!=a[i-1]+1)return 0;return 1; } A voidWorkintXintYintK) {Rep (I,0, K-1) Swap (a[x+i],a[y+i]); } - - voidDfsintXints) { the if(x==n+1) {Ans+=fac[s];return; } - intt1=0, t2=0; - for(intI=1; i<=bin[n]; i+=Bin[x]) - if(!check (i,bin[x])) { + if(!T1) t1=i; - Else if(!T2) t2=i; + Else return; A } at if(!T1) {DFS (x+1, s);return; } - if(!T2) Work (t1,t1+bin[x-1],bin[x-1]), DFS (x+1, s+1), Work (t1,t1+bin[x-1],bin[x-1]); - Else -Rep (A,0,1) Rep (b,0,1){ -Work (t1+a*bin[x-1],t2+b*bin[x-1],bin[x-1]); - if(check (t1,bin[x]) &&check (t2,bin[x])) { inDFS (x+1, s+1); -Work (t1+a*bin[x-1],t2+b*bin[x-1],bin[x-1]); to Break; + } -Work (t1+a*bin[x-1],t2+b*bin[x-1],bin[x-1]); the } * } $ Panax Notoginseng intMain () { -Freopen ("bzoj3990.in","R", stdin); theFreopen ("Bzoj3990.out","W", stdout); +scanf"%d", &n); fac[0]=bin[0]=1; ARep (I,1, the) fac[i]=fac[i-1]*i,bin[i]=bin[i-1]<<1; theRep (I,1, Bin[n]) scanf ("%d",&a[i]); +Dfs1,0); printf"%lld\n", ans); - return 0; $}
[BZOJ3990] [SDOI2015] Sort (DFS)