最大上升子序列

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

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

Sample Output

  1. 3
  2. 1
  3. 1
  4.  
  5. 题目大意:给出一串序列,要求输出最大上升子序列。
  6.  
  7. 分析:用动态规划,效率高;如果后一个数比前一个大,上升序列长度加1,否则长度减1,将后者较小的数代替前者。
  8.  
  9. 代码如下:
  10.  
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. const int maxn=100005;
  5. using namespace std;
  6. int a[maxn],dp[maxn],len,n;
  7. int find(int x)
  8. {
  9. int l=1,r=len,mid;
  10. while(l<=r)
  11. {
  12. mid=(l+r)>>1;
  13. if(x>dp[mid])
  14. l=mid+1;
  15. else
  16. r=mid-1;
  17. }
  18. return l;
  19. }
  20. int main()
  21. {
  22. while(scanf("%d",&n)==1)
  23. {
  24. len=0;
  25. for(int i=1;i<=n;i++)
  26. scanf("%d",&a[i]);
  27. for(int i=1;i<=n;i++)
  28. {
  29. int k=find(a[i]);
  30. dp[k]=a[i];
  31. len=max(len,k);
  32. }
  33. printf("%d\n",len);
  34. }
  35. return 0;
  36. }
  1.  

LIS-Program E的更多相关文章

  1. POJ3903:Stock Exchange(LIS)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/E 题目: Description The world ...

  2. HDU 1160 FatMouse's Speed(要记录路径的二维LIS)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. POJ 2533 动态规划入门 (LIS)

    Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42914 Accepte ...

  4. HDU 1069 Monkey and Banana(二维偏序LIS的应用)

    ---恢复内容开始--- Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  5. POJ 2533 Longest Ordered Subsequence(LIS模版题)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47465   Acc ...

  6. POJ 3903:Stock Exchange(裸LIS + 二分优化)

    http://poj.org/problem?id=3903 Stock Exchange Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  7. hdu----(1950)Bridging signals(最长递增子序列 (LIS) )

    Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. IGS_学习笔记05_IREP开发Concurrent Program为客户化集合接口(案例)

    20150819 Created By BaoXinjian

  9. POJ 1631 Bridging signals(LIS O(nlogn)算法)

    Bridging signals Description 'Oh no, they've done it again', cries the chief designer at the Waferla ...

  10. 动态规划——E (LIS())最长上升子序列

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

随机推荐

  1. android中textview设置为多行文本时,如何让文字从最顶开始显示

    <span style="white-space:pre"> </span><EditText android:layout_width=" ...

  2. OpenCV3编程入门笔记(3)线性滤波、非线性滤波、图像深度、通道

    15     遍历图像中的像素,是先for行数后for列数的,也就是一列一列的遍历,matlab中是从1开始计数,opnecv中采用c语言的从0开始计数. 矩阵归一化:normalize()函数,参数 ...

  3. sizeof和strlen()的区别

    二者有本质上的区别 从定义可以知道sizeof只是一个operator,而strlen()则是定义一个定义在<string.h>中的函数;所以sizeof(string)是在计算strin ...

  4. mac app icon 设置

    mac app icon 设置 1:目前 mac app 所需要的icon 图标尺寸 icon_16x16.png 16px icon_16x16@2x.png 32px icon_32x32.png ...

  5. canvas drawImage异步特性

    先看代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  6. DSP EPWM学习笔记2 - EPWM相关寄存器设置问题解析

    DSP EPWM学习笔记2 - EPWM相关寄存器设置问题解析 彭会锋 本篇主要针对不太熟悉的TZ 故障捕获 和 DB 死区产生两个子模块进行学习研究 感觉TI的寄存器命名还是有一定规律可循的 SEL ...

  7. hdu 4034 Graph (floyd的深入理解)

    Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submi ...

  8. 离线网页制作器(beta1.0)

    package hhuarongdao; /* *使用方法: 先选择保存路径,然后输入相应的网址, *然后会得到那个网页的离线版的 内容 * */ import java.awt.BorderLayo ...

  9. poj------(3468)A Simple Problem with Integers(区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 60745   ...

  10. JavaSE基础之this关键字的引用

    1.0   this 指代当前对象, 在一般方法中可以通过this来引用当前对象的成员(方法,属性). 2.0  通过  this()  调 用重载的构造器,需要注意的是,通过此种方法调用的重载构造器 ...