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. codeforces 540D 概率dp

    传送门 大概可以这样理解, 一开始有r个石头, p个布, s个剪刀, 每一天有其中的两个相遇, 如果两个是相同的种类, 什么都不会发生, 否则的话有一个会挂掉, 问最后每一种生存的概率. dp[i][ ...

  2. :last-child的诡异的问题!!

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Mysql 忘密码 + Phpadmin 修改密码无法登陆

    mysql有时候忘记密码了怎么办?我给出案例和说明!一下就解决了! Windows下的实际操作如下 1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysql ...

  4. 清理8组nodes中表的历史数据,平均每个node中的表有1.5亿条记录,需要根据date_created字段清理8000W数据记录,这个字段没有索引。

    清理8组nodes中表的历史数据,平均每个node中的表有1.5亿条记录,需要根据date_created字段清理8000W数据记录,这个字段没有索引. 环境介绍  线上磁盘空间不足,truncate ...

  5. oracle 11g RAC ocfs2

    http://oracle-base.com/articles/linux/ocfs2-on-linux.php http://oracle-base.com/articles/11g/oracle- ...

  6. Database(Mysql)发版控制二

    author:skate time:2014/08/18 Database(Mysql)发版控制 The Liquibase Tool related Database 一.Installation ...

  7. Google Maps 学习笔记(二)地图天气预报服务 2014.06.04

    地图天气预报服务:一,获取天气预报信息:二,解析天气预报信息:三,在地图上加载天气预报信息: Yahoo!提供的天气预报服务采用流行的RSS输出结果,接口地址如下: http://weather.ya ...

  8. (转)ios跳转到通用页面

    在代码中调用如下代码: [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"prefs:root=LOCATION ...

  9. spring 加载配置文件的相关配置总结

    PropertyPlaceholderConfigurer      注意: Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:p ...

  10. mysq数据库管理工具navicat基本使用方法

    navicat是mysql数据库的客户端查询管理工具,本文详细的介绍了该软件的基本使用方法 本文转自 http://hejiawangjava.iteye.com/blog/2245758 sql是操 ...