POJ3903 5thweek.problem_E LIS

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.

题目分析:

依然是求最长上升子序列问题。方法同B题zoj1093.

提醒自己注意两个方面:1.子序列不要求连续。2、数组越界问题和小于等于的取舍问题。

代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int stock[100005],dp[100005];
 5 int main()
 6 {
 7     int n,i,k,left,right,mid;
 8   while(scanf("%d",&n)!=EOF)
 9   {
10       for(i=0;i<n;i++)
11         scanf("%d",&stock[i]);
12      dp[0]=-1;
13      k=0;
14         for(i=0;i<n;i++)
15         {
16             if(stock[i]>dp[k])
17                 dp[++k]=stock[i];
18             else
19             {
20                 left=1;
21                 right=k;
22                 while(left<=right)
23               {
24                  mid=(left+right)/2;
25                  if(stock[i]>dp[mid])
26                     left=mid+1;

27                  else
28                     right=mid-1;
29               }
30               dp[left]= stock[i];
31             }
32
33         }
34         printf("%d\n",k);
35   }
36   return 0;
37 }
时间: 2024-10-10 17:16:56

POJ3903 5thweek.problem_E LIS的相关文章

POJ3903 Stock Exchange LIS最长上升子序列

POJ3903 Stock Exchange 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 1e5+5; 7 int a[maxn]; 8 int main() { 9 int n; 10 while (~scanf("%d",&am

5thweek.problem_B(zoj 1093) LIS

Description 一组研究人员正在设计一项实验,以测试猴子的智商.他们将挂香蕉在建筑物的屋顶,同时,提供一些砖块给这些猴子.如果猴子足够聪明,它应当能够通过合理的放置一些砖块建立一个塔,并爬上去吃他们最喜欢的香蕉. 研究人员有n种类型的砖块,每种类型的砖块都有无限个.第i块砖块的长宽高分别用xi,yi,zi来表示. 同时,由于砖块是可以旋转的,每个砖块的3条边可以组成6种不同的长宽高. 在构建塔时,当且仅当A砖块的长和宽都分别小于B砖块的长和宽时,A砖块才能放到B砖块的上面,因为必须留有一

POJ3903:Stock Exchange(LIS)

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

poj3903 LIS的优化

题意:最长递增子序列 思路:二分优化 理解dp数组存的东西是关键 dp数组的值可能会更新 代码: #include <algorithm> #include <iostream> #include <sstream> #include <cstdlib> #include <cstring> #include <iomanip> #include <cstdio> #include <string> #incl

poj3903 Stock Exchange最大上升子序列

转载请注明出处:http://blog.csdn.net/u012860063 题目链接: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 ev

小明系列问题――小明序列(LIS)

小明系列问题――小明序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4521 Description 大家都知道小明最喜欢研究跟序列有关的问题了,可是也就因为这样,小明几乎已经玩遍各种序列问题了.可怜的小明苦苦地在各大网站上寻找着新的序列问题,可是找来找去都是自己早已研究过的序列.小明想既然找不到,那就自己来发明一个新的序列问题吧!

hdu4521 小明系列问题——小明序列(LIS变种 (线段树+单点更新解法))

链接: huangjing 题目:中文题目 思路: 这个题目如果去掉那个距离大于d的条件,那么必然是一个普通的LIS,但是加上那个条件后就变得复杂了.用dp的解法没有看懂,我用的线段树的解法...就是采用延迟更新的做法,用为距离要大于d啊,所以我们在循环到第i的时候,就对(i-d-1)这个点进行更新,因为如果在(i-d-1)这个点更新了,会对后面的造成影响,然后线段树的tree[]数组存的是以i结尾的最长lis,那么每次询问的时候就找最大的tree[]就可以了... 代码: 小明系列问题--小明

RQNOJ 201 奥运大包围:LIS + 拼链成环

题目链接:https://www.rqnoj.cn/problem/201 题意: 开始时n(n<=1000)个人手拉手围成一个圈. 后来这些人中的一些按顺序向里面出圈形成一个新圈.从而使原圈形成一个从高到低,最低与最高连接的圈. 新圈重复相同的操作,直到没有人要出圈为止. 问最少要形成多少个这样的圈. 题解: (1)拼链成环: 对于一个环,可以用两条由环拆开的链拼在一起表示. 例如:有一个环为"1,2,3,4"(1和4连在一起),则可以表示为"1,2,3,4,1,2,

Gym 101246H ``North-East&#39;&#39;(LIS)

http://codeforces.com/gym/101246/problem/H 题意: 给出n个点的坐标,现在有一个乐队,他可以从任一点出发,但是只能往右上方走(包括右方和上方),要经过尽量多的点.输出它可能经过的点和一定会经过的点. 思路: 分析一下第一个案例,在坐标图上画出来,可以发现,他最多可以经过4个点,有两种方法可以走. 观察一下,就可以发现这道题目就是要我们求一个LIS. 首先,对输入数据排一下顺序,x小的排前,相等时则将y大的优先排前面. 用二分法求LIS,这样在d数组中就可