zoukankan      html  css  js  c++  java
  • hdu 3998 Sequence

    There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequence of X
    as x[i1], x[i2],...,x[ik], which satisfies follow conditions:
    1) x[i1] < x[i2],...,<x[ik];
    2) 1<=i1 < i2,...,<ik<=n

    As an excellent program designer, you must know how to find the maximum length of the
    increasing sequense, which is defined as s. Now, the next question is how many increasing
    subsequence with s-length can you find out from the sequence X.

    For example, in one case, if s = 3, and you can find out 2 such subsequence A and B from X.
    1) A = a1, a2, a3. B = b1, b2, b3.
    2) Each ai or bj(i,j = 1,2,3) can only be chose once at most.

    Now, the question is:
    1) Find the maximum length of increasing subsequence of X(i.e. s).
    2) Find the number of increasing subsequence with s-length under conditions described (i.e. num).

    InputThe input file have many cases. Each case will give a integer number n.The next line will
    have n numbers.OutputThe output have two line. The first line is s and second line is num.Sample Input
    4
    3 6 2 5
    Sample Output
    2
    2

    最长上升子序列。
    代码:
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <map>
    #define Max 1000
    using namespace std;
    int n,s[Max],vis[Max];///vis标记是否已经使用
    int maxl()
    {
        int res = 0,t[Max];
        for(int i = 0;i < n;i ++)
        {
            if(vis[i])continue;
            if(!res || t[res - 1] < s[i])
            {
                t[res ++] = s[i];
                vis[i] = 1;
            }
            else
            {
    //            *lower_bound(t,t + res,s[i]) = s[i];
                int l = 0,r = res,mid;
                while(l < r)
                {
                    mid = (l + r) / 2;
                    if(t[mid] >= s[i])r = mid;
                    else l = mid + 1;
                }
                t[l] = s[i];
            }
        }
        return res;
    }
    int main()
    {
        while(scanf("%d",&n) != EOF)
        {
            for(int i = 0;i < n;i ++)
            {
                scanf("%d",&s[i]);
            }
            memset(vis,0,sizeof(vis));
            int m = maxl(),c = 1;
            while(maxl() == m)c ++;
            printf("%d
    %d
    ",m,c);
        }
    }
  • 相关阅读:
    c++错误:不允许使用抽象类类型 "Employee" 的对象
    C++ error C2027:使用了未定义类型 类的调用顺序
    PyCharm 2020.1 激活教程
    mysql组内排序
    XGBoost
    React学习——Hello, React
    Lambda表达式
    plsql链接远程oracle服务器,以及常用配置
    静态网站 H5 跳小程序 (短信跳小程序)
    更新(D-U-N-S)邓白氏码公司信息(注册勿看)
  • 原文地址:https://www.cnblogs.com/8023spz/p/9008803.html
Copyright © 2011-2022 走看看