摆动序列(DP)

 算法训练 摆动序列  
时间限制:1.0s   内存限制:512.0MB
       
问题描述
  如果一个序列满足下面的性质,我们就将它称为摆动序列:
  1. 序列中的所有数都是不大于k的正整数;
  2. 序列中至少有两个数。
  3. 序列中的数两两不相等;
  4. 如果第i – 1个数比第i – 2个数大,则第i个数比第i – 2个数小;如果第i – 1个数比第i – 2个数小,则第i个数比第i – 2个数大。
  比如,当k = 3时,有下面几个这样的序列:
  1 2
  1 3
  2 1
  2 1 3
  2 3
  2 3 1
  3 1
  3 2
  一共有8种,给定k,请求出满足上面要求的序列的个数。
输入格式
  输入包含了一个整数k。(k<=20)
输出格式
  输出一个整数,表示满足要求的序列个数。
样例输入
3
样例输出
8
感觉这题不能说啊,,说起来都是泪啊!!!@@#%@%


还是上代码吧:

第一个是别人做的

#include <stdio.h>
#include <math.h>
  
int main()
{
    int k;
    scanf("%d", &k);
    printf("%d", (int)(pow(2, k) - k - 1) * 2);
    return 0;
}


下面是我的:

# include <stdio.h>
# include <memory.h>
int a[11111];
int c[11111];
int count=0;
int n;
int max;
int min;
int main()
{
    int i,j,temp;
    
    scanf("%d", &n);
    max = n;
    min = 1;
    int k,h;
    int cnt;
    int t[1111];
    for (i=1; i<=n; i++)
        c[i] = i;
    for (i=1; i<=n; i++)
    {
        a[1]=i;
        for (j=1; j<=n; j++)
        {
            if (i==j)
                continue;
            if (a[1]==max || a[1]==min)
            {
                count++;
                continue;
            }
            
            a[2] = j;
            cnt = 2;
            k=1;
            h=1;
            count++;
            memset(t, 0, sizeof(t));
            while (cnt<=n)
            {           
                if ((a[cnt-1]==max || a[cnt-1]==min) && h==1)
                    break;
                if ((a[cnt-1]==max || a[cnt-1]==min) && h!=1)           
                {
                    cnt = t[--h];
                    t[h]=0;
                    if (a[cnt-2]>a[cnt-1])
                    {
                        a[cnt] = a[cnt]+1;
                        if (a[cnt] != max)
                            t[h++] = cnt;
                        count++;
                    }   
                    else
                        if (a[cnt-2]<a[cnt-1])
                        {
                            a[cnt] = a[cnt]-1;
                            if (a[cnt] != min)
                                t[h++] = cnt;
                            count++;
                        }   
                }
                if ((a[cnt-1]==max || a[cnt-1]==min) && h==1)
                    break;  
                if (a[cnt-1] > a[cnt] && a[cnt-1]!=max)
                {
                    count++;
                    a[cnt+1] = a[cnt-1]+1;
                    if (a[cnt+1] != max)
                        t[h++] = cnt+1;
                    cnt++;
                }
                else if (a[cnt-1] < a[cnt] && a[cnt-1]!=min)
                {
                    count++;
                    a[cnt+1] = a[cnt-1]-1;
                    if (a[cnt+1] != min)
                        t[h++] = cnt+1;
                    cnt++;
                }
                if (a[cnt-1]==max || a[cnt-1]==min)
                {
                    if (h > 1)
                    {
                        cnt = t[--h];
                        t[h]=0;
                        if (a[cnt-2]>a[cnt-1])
                        {
                            a[cnt] = a[cnt]+1;
                            if (a[cnt] != max)
                                t[h++] = cnt;
                            count++;
                        }   
                        else
                            if (a[cnt-2]<a[cnt-1])
                            {
                                a[cnt] = a[cnt]-1;
                                if (a[cnt] != min)
                                    t[h++] = cnt;
                                count++;
                            }   
                    }   
                }
            }
        }
    }   
    printf("%d", count);
    
    return 0;
}



评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值