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

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/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

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
 
分析:
1.LIS。求最长上升子序列
2. 要考虑时间复杂度。因为n为1000000,如果用n^2的算法肯定超时,所以要选择nlogn的算法。(百度的),用二分找最优值
3.用cnt保存最长上升子序列的值。b[cnt++]=a[i]
 
代码:
 #include<cstdio>
#include<iostream>
using namespace std; const int maxn=; int i;
int n,cnt;
int a[maxn],b[maxn]; int LIS()
{
cnt=;
b[]=;
for(i=;i<n;i++)
{
if(cnt==||a[i]>b[cnt])
{
cnt++;
b[cnt]=a[i]; //保存最长上升子序列的个数
}
else
{
int l=,r=cnt;
while(l<=r) //二分比较a[]和b[]中值的大小关系
{
int mid=(l+r)/;
if(a[i]>b[mid])
l=mid+;
else
r=mid-;
}
b[l]=a[i]; }
}
return cnt;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(i=;i<n;i++)
scanf("%d",&a[i]);
LIS();
printf("%d\n",cnt);
}
return ;
}

开始调试的时候,只要输入一个数就输出1,一直没有找出来错误,找别人帮忙来看也没有找出,后来才发现是因为写的时候把scanf中的%d写成了d%,真是好坑啊。。。。。

下次一定要好好注意这些小地方

 
 

POJ 3903 Stock Exchange (E - LIS 最长上升子序列)的更多相关文章

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

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

  2. Poj 3903 Stock Exchange(LIS)

    一.Description The world financial crisis is quite a subject. Some people are more relaxed while othe ...

  3. POJ 3903 Stock Exchange(LIS || 线段树)题解

    题意:求最大上升子序列 思路:才发现自己不会LIS,用线段树写的,也没说数据范围就写了个离散化,每次查找以1~a[i]-1结尾的最大序列答案,然后更新,这样遍历一遍就行了.最近代码总是写残啊... 刚 ...

  4. LIS(nlogn) POJ 3903 Stock Exchange

    题目传送门 题意:LIS最长递增子序列 O(nlogn) 分析:设当前最长递增子序列为len,考虑元素a[i]; 若d[len]<a[i],则len++,并使d[len]=a[i]; 否则,在d ...

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

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

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

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

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

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

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

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

  9. POJ 3903 Stock Exchange

    Stock Exchange Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2954   Accepted: 1082 De ...

随机推荐

  1. 【LeetCode题意分析&解答】43. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  2. python 关于dict的一些总结

    总结了一些关于字典的小技巧或者注意的地方. 使用zip创建字典 创建字典有以下三种方法 dict(a=1, b=2, c=2) dict([(a,1), (b,2), (c,3)]) dict({a: ...

  3. Eclipse中搭建Python开发环境

    转载:http://www.cnblogs.com/realh/archive/2010/10/04/1841907.html Eclipse+PyDev环境搭建 1.准备工作 JDK6 Java 开 ...

  4. JAVA并发,锁与方法

    引自:<thinking in java> synchronized void f(){/* ... */}; synchronized void g(){/* ... */}; 所有对象 ...

  5. CCNA实验(3) -- RIP

    RIP协议分为版本1和版本2,均具备以下特征:1.是距离向量路由协议2.使用跳数(Hop Count)作为度量值3.默认路由更新周期为30秒4.管理距离(AD)为1205.支持触发更新6.最大跳数为1 ...

  6. nyist 202 红黑树(二叉树中序遍历)

    旋转对中序遍历没有影响,直接中序输出即可. #include <iostream> #include <cstdio> using namespace std; int n; ...

  7. xcode 不值钱的动画UIImageView

    了解 animateWithDuration方法 制作动画变得不值钱 代码创建一个UIImageView 后加入self.view 容器中 调用点击屏幕touchesBegan 方法执行动画 #imp ...

  8. .net mvc System.Web.Optimization 、System.Data.Entity.Infrastructure找不到

    在MVC4的开发中,如果在App_Start目录下BundleConfig.cs类没有找不到引用System.Web.Optimization,可以使用程序包管理控制台进行安装到使用的项目 打开 工具 ...

  9. git版本工具(团队开发常用)

    1.创建一个版本库 mkdir repository    //创建一个文件夹 git init        //把目录编程git可以管理的仓库 2.提交文件到版本库 git add test.tx ...

  10. ORACLE 计算节假日

    create or replace function get_workday(starTime in date,endTime in date) return number is Weekends n ...