E - LIS

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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.
解题思路:这个题目是一个求最长递增子序列问题,找一个数组存输入的最大升序,然后它的长度就是我们需要输出的答案需,要用二分忧化法做,

我们的问题是要如何找到最大升序数组?这里可以分成两步:1.判断当前数字时候大于前一数字,若是:把它放在数组后面,否则:通过2分法,判断它的大小应该放的位置即可。
程序代码:
#include <cstdio>
using namespace std;
const int L=;
int a[L],b[L];
int n;
void init()
{
for(int i=;i<n;i++)
scanf("%d",&a[i]);
}
int bin(int r,int k)
{
int l=;
while(l<=r)
{
int mid=(l+r)/;
if(k>b[mid])
l=mid+;
else
r=mid-;
}
return l;
}
int work()
{
int i,j,k;
int c=;
for(i=;i<n;i++)
if(c==||a[i]>b[c])
b[++c]=a[i];
else
{
k=bin(c,a[i]);
b[k]=a[i];
}
return c;
}
int main()
{
while(scanf("%d",&n)==)
{
init();
printf("%d\n",work());
}
return ;
}

动态规划——E (LIS())最长上升子序列的更多相关文章

  1. 动态规划模板1|LIS最长上升子序列

    LIS最长上升子序列 dp[i]保存的是当前到下标为止的最长上升子序列的长度. 模板代码: int dp[MAX_N], a[MAX_N], n; int ans = 0; // 保存最大值 for ...

  2. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

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

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

  4. hdu 5256 序列变换(LIS最长上升子序列)

    Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数. 请输出最少需要修改多 ...

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

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

  6. POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)

    POJ 1887Testingthe CATCHER (LIS:最长下降子序列) http://poj.org/problem?id=3903 题意: 给你一个长度为n (n<=200000) ...

  7. 300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  8. 动态规划(DP),最长递增子序列(LIS)

    题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(d ...

  9. 低价购买 (动态规划,变种最长下降子序列(LIS))

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

随机推荐

  1. JS根据key值获取URL中的参数值,以及把URL的参数转换成json对象

    //把url的参数部分转化成json对象 parseQueryString: function (url) { var reg_url = /^[^\?]+\?([\w\W]+)$/, reg_par ...

  2. HBuilder使用感受

    最近公司在考虑搞HTML5和后台交互的架构,我于是便下载了HBuilder使用,这里分享下我偶的使用感受. 一.首先,下载下来是一个压缩包,解压后是可以直接使用的,这让我对它的第一感觉很好.不用安装, ...

  3. SQLite 入门教程(三)好多约束 Constraints

    一.约束 Constraints 在上一篇随笔的结尾,我提到了约束, 但是在那里我把它翻译成了限定符,不太准确,这里先更正一下,应该翻译成约束更贴切一点. 那么什么是约束呢? 我们在数据库中存储数据的 ...

  4. mysql locktables

    SELECT      r.trx_id waiting_trx_id,      r.trx_mysql_thread_id waiting_thread,      TIMESTAMPDIFF(  ...

  5. CentOS 6.5下搭建NFS文件服务器

    环境介绍:服务器: 192.168.0.1客户机: 192.168.0.2安装软件包:服务器和客户机都要安装nfs 和 rpcbind 软件包:yum -y install nfs-utils rpc ...

  6. 数据库导出导入操作(expdp,impdp)

    EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用. 命令行: sqlplus/nolog connect username/password as sysd ...

  7. C#操作求出SQL中某一字段所有行的和方法!

    DataTable table = xx.sqlcha(sql1);//调数据库 ; foreach(DataRow row in table.Rows)//遍历所查出记录所有行 { v = v + ...

  8. 读终端输入数据BufferedReader

    public static void main(String[] args) {        BufferedReader br=new BufferedReader(new InputStream ...

  9. 《用chsh选择shell》-linux命令五分钟系列之十二

    chsh命令用于修改你的登录shell. 1 我想知道我机器安装了哪些shell? 两种方法可以查看: 第一种: [rocrocket@wupengchong ~]$ chsh -l /bin/sh ...

  10. CSS3 Animation学习笔记

    Internet Explorer 9,以及更早的版本, 不支持 @keyframe 规则或 animation 属性. Internet Explorer 10.Firefox 以及 Opera 支 ...