转载请注明出处:http://blog.csdn.net/u012860063

题目链接:

id=3903">http://poj.org/problem?id=3903

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

第一种:http://blog.csdn.net/u012860063/article/details/34086819

代码例如以下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int a[100047],dp[100047],n; int bin(int size,int k)
{
int l = 1,r = size;
while(l<=r)
{
int mid = (l+r)/2;
if(k>dp[mid])
l = mid+1;
else
r = mid-1;
}
return l;
} int LIS(int *a)
{
int i,j,ans=1;
dp[1] = a[1];
for(i = 2; i<=n; i++)
{
if(a[i]<=dp[1])
j = 1;
else if(a[i]>dp[ans])
j = ++ans;
else
j = bin(ans,a[i]);
dp[j] = a[i];
}
return ans;
}
int main()
{
int i;
while(~scanf("%d",&n))
{
for(i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
}
int ans = LIS(a);
printf("%d\n",ans);
}
return 0;
}

另外一种使用了:lower_bound

代码例如以下:

#include<cstdio>
#include<algorithm>
using namespace std;
#define INF 0x3fffffff int a[100047],dp[100047]; int main()
{
int t,i;
while(scanf("%d",&t)!=EOF)
{
for(i = 0; i <= t; i++)
dp[i]=INF;
for(i = 0; i < t; i++)
{
scanf("%d",&a[i]);
}
for(i = 0; i < t; i++)
{
*lower_bound(dp,dp+t,a[i])=a[i];
}
printf("%d\n",lower_bound(dp,dp+t,INF)-dp);
}
return 0;
}

附上解释的图片一张:

poj3903 Stock Exchange(最长上升子序列)的更多相关文章

  1. POJ 3903 Stock Exchange 最长上升子序列入门题

    题目链接:http://poj.org/problem?id=3903 最长上升子序列入门题. 算法时间复杂度 O(n*logn) . 代码: #include <iostream> #i ...

  2. POJ3903 Stock Exchange LIS最长上升子序列

    POJ3903 Stock Exchange #include <iostream> #include <cstdio> #include <vector> #in ...

  3. poj3903 Stock Exchange 二分+dp

    题目地址:http://poj.org/problem?id=3903 题目: Description The world financial crisis is quite a subject. S ...

  4. POJ - 3903 Stock Exchange(LIS最长上升子序列问题)

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

  5. POJ 3903 Stock Exchange (E - LIS 最长上升子序列)

    POJ 3903    Stock Exchange  (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...

  6. {POJ}{3903}{Stock Exchange}{nlogn 最长上升子序列}

    题意:求最长上升子序列,n=100000 思路:O(N^2)铁定超时啊....利用贪心的思想去找答案.利用栈,每次输入数据检查栈,二分查找替换掉最小比他大的数据,这样得到的栈就是更优的.这个题目确实不 ...

  7. poj 3903 Stock Exchange(最长上升子序列,模版题)

    题目 #include<stdio.h> //最长上升子序列 nlogn //入口参数:数组名+数组长度,类型不限,结构体类型可以通过重载运算符实现 //数组下标从1号开始. int bs ...

  8. POJ 3903 Stock Exchange 【最长上升子序列】模板题

    <题目链接> 题目大意: 裸的DP最长上升子序列,给你一段序列,求其最长上升子序列的长度,n^2的dp朴素算法过不了,这里用的是nlogn的算法,用了二分查找. O(nlogn)算法 #i ...

  9. POJ3903:Stock Exchange(LIS)

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

随机推荐

  1. Ubuntu Linux 永山(mount)分

    在一般情况下,我们想安装一个分区解决方案是使用mount命令,因为我想/dev/sda3安装/media/aborn/data通过使用下面的命令 sudo mount /dev/sda3 /media ...

  2. WPF DataGrid_SelectChanged获取单元内容

    private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)        {          ...

  3. php学习笔记--error

    不同的错误处理方法: 简单的die()语句 自己定义错误函数和错误触发器 错误报告 主要的错误处理:使用die()函数 if(!file_exists("welcome.txt") ...

  4. DevExpress控件中LayoutControl的使用

    原文:DevExpress控件中LayoutControl的使用 C#开发中,软件布局设计,主要用TableLayoutPanel能很好地支持缩放功能,对自身的Label.TextBox等控件支持的很 ...

  5. 玩转web之ajax(一)---使用表单的serialize()方法中文乱码解决

    有时候我们需要使用ajax提交去提交form的值,这样就需要使用serialize()去获取form的值,但这样获取的值如果有中文,会乱码,原因和解决方法如下: 原因:.serialize()自动调用 ...

  6. mac_Mac环境下怎样编写HTML代码?

    在Mac环境下,使用默认的文本编辑器编写的HTML的源代码, 使用不同的浏览器打开后,依旧还是显示源代码 推荐使用UltraEdit,问题就迎刃而解了

  7. [文学阅读] METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments

    METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments Satanje ...

  8. adapter pattern

    对象适配器 9.7 适配器模式总结 适配器模式将现有接口转化为客户类所期望的接口,实现了对现有类的复用,它是一种使用频率非常高的设计模式,在软件开发中得以广泛应用,在Spring等开源框架.驱动程序设 ...

  9. 【原创翻译】认识MVC设计模式:web应用开发的基础(实际编码篇)

    原文地址:http://www.larryullman.com/2009/10/15/understanding-mvc-part-3/ 全系列INDEX [原创翻译]认识MVC设计模式:web应用开 ...

  10. 改动symbol link的owner

    当/home/jenkins文件夹空间不足的时候,能够先查看哪个文件夹在较大的磁盘分区上,然后将jenkins文件夹移动过去 最后创建/home/jenkins link到新位置. 这时候须要改动sy ...