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);
        }
    }
  • 相关阅读:
    android UI 适配小节
    Android中的倒计时实现
    几种适配器&观察者&ListView之间的那点事
    Service stopSelf(int statId)和onStartcommand(Intent intent,int flags,int startId)
    java 虚函数
    java 重载和多态的区别
    小程序体验版路径以及参数携带
    微信小程序--上传图片公用组件
    微信小程序页面返回
    js兼容安卓和IOS的复制文本到剪切板
  • 原文地址:https://www.cnblogs.com/8023spz/p/9008803.html
Copyright © 2011-2022 走看看