zoukankan      html  css  js  c++  java
  • POJ

    E - LIS
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
     

    Description

    The world financial crisis is quite a subject. Some people are more relaxed while others are quite anxious. John is one of them. He is very concerned about the evolution of the stock exchange. He follows stock prices every day looking for rising trends. Given a sequence of numbers p1, p2,...,pn representing stock prices, a rising trend is a subsequence pi1 < pi2 < ... < pik, with i1 < i2 < ... < ik. John’s problem is to find very quickly the longest rising trend.

    Input

    Each data set in the file stands for a particular set of stock prices. A data set starts with the length L (L ≤ 100000) of the sequence of numbers, followed by the numbers (a number fits a long integer). 
    White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

    Output

    The program prints the length of the longest rising trend. 
    For each set of data the program prints the result to the standard output from the beginning of a line.

    Sample Input

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

    Sample Output

    3 
    1 
    1

    Hint

    There are three data sets. In the first case, the length L of the sequence is 6. The sequence is 5, 2, 1, 4, 5, 3. The result for the data set is the length of the longest rising trend: 3.
     
     
    题解:LIS最长上升子序列问题  给出一个序列,从左到右的顺序选出尽量多的整数,组成一个上升子序列。
     
    #include<cstdio>
    int a[100001], f[100001];
    int main()
    {
        int n, k, l, r, mid;
        while (scanf("%d",&n)==1)
        {
            for (int i = 0; i < n; i++)
                scanf("%d",&a[i]);
            k = 0;
            f[0] = -1;             //赋初值,小于0即可
            for (int i = 0; i < n; i++)
            {
                if (a[i] > f[k])
                {
                    k++;
                    f[k] = a[i];         //每找到一个就保存到f【】数组里
                }
                else
                {
                    l = 1, r = k;
                    while (l<=r)          //判断此时的a[i]和f数组中各个值大小关系,直到找到最优值
                    {
                        mid = (l + r) / 2;
                        if (a[i] > f[mid])
                            l = mid + 1;
                        else
                            r = mid - 1;
                    }
                    f[l] = a[i];
                }
            }
            printf("%d
    ",k);
        }
    
    }
  • 相关阅读:
    【毒链剖分】
    线段树——从入门到入土
    二分图最大匹配——找老婆问题(确信)
    【AC自动机(有了这个我就能AC了吗)】
    社交距离II
    【扩展KMP(Z函数)(这个区间看着不是很爽)】
    【KMP算法(烤馍片,真香)】
    【最短路系列问题】
    最近公共祖先LCA(我肯定是你的LCA)
    【并查集(又在乱牵线了)】
  • 原文地址:https://www.cnblogs.com/hfc-xx/p/4725490.html
Copyright © 2011-2022 走看看