Algorithm Training Swing Sequence

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.