zoukankan      html  css  js  c++  java
  • LIS-Program E

    最大上升子序列

     

    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


    题目大意:给出一串序列,要求输出最大上升子序列。

    分析:用动态规划,效率高;如果后一个数比前一个大,上升序列长度加1,否则长度减1,将后者较小的数代替前者。




    代码如下:


    #include <iostream>
    #include <cstdio>
    #include <algorithm> 
    const int maxn=100005;
    using namespace std;
    int a[maxn],dp[maxn],len,n;
    int find(int x)
    {
    	int l=1,r=len,mid;
    	while(l<=r)
    	{
    		mid=(l+r)>>1;
    		if(x>dp[mid])
    			l=mid+1;
    		else
    			r=mid-1;
    	}
    	return l;
    }
    int main()
    {
    	while(scanf("%d",&n)==1)
    	{
    		len=0;
    		for(int i=1;i<=n;i++)
    			scanf("%d",&a[i]);
    		for(int i=1;i<=n;i++)
    		{
    			int k=find(a[i]);
    			dp[k]=a[i];
    			len=max(len,k);
    		}
    		printf("%d
    ",len);			
    	}
    	return 0; 
    }





  • 相关阅读:
    npm ci命令解析
    performance分析
    mpvue 引入直播功能
    lodash按需加载
    React生命周期分析
    vue 项目打包优化(远不止dll)
    Git 底层数据结构和原理
    Docker 部署 Nuxt.js 项目
    TMS320DM642调试出现#10247-D creating output section ".capChaACrSpace" without a SECTIONS 解决办法
    TMS320DM642的emif(外部存储器接口)的结构设置
  • 原文地址:https://www.cnblogs.com/xl1164191281/p/4734946.html
Copyright © 2011-2022 走看看