zoukankan      html  css  js  c++  java
  • SDUSTOJ 1800 LIS(最长上升子序列不同位置个数)

    Description

    给定一个长度为n的序列(n <= 1000) ,记该序列LIS(最长上升子序列)的长度为m,求该序列中有多少位置不相同的长度为m的严格上升子序列。

    Input

    先输入一个T,表明下面会有几组数据。
    每组数据先输入一个n,表明数组大小。
    下面一行有n个数代表该数组,且 1<=a[i]<=n;

    Output

    输出为一行,只需输出上述要求的个数(输出保证结果小于2^31)。

    Sample Input

    3
    2
    1 2
    4
    1 2 4 4
    5 
    1 2 3 4 5

    Sample Output

    1
    2
    1
    思路:
    还是第三届山科校赛的题,这个题算是比较水了
    就是在求最长上升子序列的时候用数组维护一个以当前位置为末尾的最长上升子序列的不同位置个数之和
    然后用另一个数组维护一个当前长度自序列的总和
    依旧是n^2的复杂度
    /* ***********************************************
    Author        :devil
    Created Time  :2016/4/26 21:58:52
    ************************************************ */
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <cmath>
    #include <stdlib.h>
    using namespace std;
    const int N=1010;
    int dp[N],a[N],b[N],c[N];
    int main()
    {
        //freopen("in.txt","r",stdin);
        int t,n;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(int i=1; i<=n; i++)
                scanf("%d",&a[i]);
            int ma=0;
            memset(c,0,sizeof(c));
            c[1]=n;
            for(int i=1; i<=n; i++)
            {
                b[i]=dp[i]=1;
                for(int j=1; j<i; j++)
                {
                    if(a[i]>a[j]&&dp[i]<dp[j]+1)
                    {
                        dp[i]=dp[j]+1;
                        b[i]=b[j];
                    }
                    else if(a[i]>a[j]&&dp[i]==dp[j]+1) b[i]+=b[j];
                }
                if(dp[i]>1) c[dp[i]]+=b[i];
                ma=max(ma,dp[i]);
            }
            printf("%d
    ",c[ma]);
        }
        return 0;
    }


  • 相关阅读:
    151. 翻转字符串里的单词(trim函数与split函数的使用)
    Java splt方法,按照空格分割字符串成为字符串数组(转载)
    137. 只出现一次的数字 II(没完全按要求做)
    129. 求根到叶子节点数字之和(递归
    125. 验证回文串(replaceAll()与toLowerCase())
    美团上海Java实习(已offer)面经(还没写完,转载)
    二叉树中序遍历
    优先队列/最大堆
    防御式编程
    JWT
  • 原文地址:https://www.cnblogs.com/d-e-v-i-l/p/5436755.html
Copyright © 2011-2022 走看看