Algorithm Training Oscillation sequence problem description if a sequence satisfies the following properties, we call it a oscillating sequence:
1. All the numbers in the sequence are positive integers that are not greater than k;
2. There are at least two numbers in a sequence.
3. The number of 22 in the sequence is not equal;
4. If the number of i–1 is greater than the number of i–2, then the number of I is smaller than the number of i–2 and if the number of i–1 is smaller than the number of i–2, then the number of I is greater than the number of i–2.
For example, when k = 3 o'clock, there are several such sequences:
1 2
1 3
2 1
2 1 3
2 3
2 3 1
3 1
3 2
A total of 8, given K, the number of requests to meet the above requirements of the sequence. The input format input contains an integer k. The (k<=20) output format outputs an integer that represents the number of sequences that satisfy the requirement. Sample input 3 Sample output 8 The ability is too slag to imagine how DP = = ... DFS implementations:
1#include <cstdio>2#include <string.h>3#include <algorithm>4 using namespacestd;5 intN, ans;6 BOOLmark[ -];7 inta[ -];8 9 voidDfsinttop)Ten { One if(top>1) Aans++; - for(intI=1; i<=n; i++) - { the if(mark[i]==0) - { - if(top>1) - { + if((a[top-1]>a[top-2] && i<a[top-2]) || (a[top-1]<a[top-2] && i>a[top-2]) ) - { +mark[i]=1; Aa[top]=i; attop++; - dfs (top); -mark[i]=0; -top--; - } - } in Else - { tomark[i]=1; +a[top]=i; -top++; the dfs (top); *mark[i]=0; $top--;Panax Notoginseng } - } the } + } A the intMain () + { -Memset (A,0,sizeof(a)); $memset (Mark,0,sizeof(Mark)); $ -ans=0; - while(SCANF ("%d", &n) = =1) the { - if(n>=2)Wuyi { theDfs0); - } Wuprintf"%d\n", ans); - } About return 0; $}
Code for others online:
1#include <stdio.h>2#include <math.h>3 4 intMain ()5 {6 intK;7scanf"%d", &k);8printf"%d", (int) (POW (2, K)-K-1) *2);9 return 0;Ten}
Algorithm Training Swing Sequence