poj3903 Stock Exchange(最长上升子序列)
转载请注明出处:http://blog.csdn.net/u012860063
题目链接: id=3903">http://poj.org/problem?id=3903
Description
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
White spaces can occur freely in the input. The input data are correct and terminate with an end of file.
Output
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(最长上升子序列)的更多相关文章
- POJ 3903 Stock Exchange 最长上升子序列入门题
题目链接:http://poj.org/problem?id=3903 最长上升子序列入门题. 算法时间复杂度 O(n*logn) . 代码: #include <iostream> #i ...
- POJ3903 Stock Exchange LIS最长上升子序列
POJ3903 Stock Exchange #include <iostream> #include <cstdio> #include <vector> #in ...
- poj3903 Stock Exchange 二分+dp
题目地址:http://poj.org/problem?id=3903 题目: Description The world financial crisis is quite a subject. S ...
- POJ - 3903 Stock Exchange(LIS最长上升子序列问题)
E - LIS Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descripti ...
- POJ 3903 Stock Exchange (E - LIS 最长上升子序列)
POJ 3903 Stock Exchange (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...
- {POJ}{3903}{Stock Exchange}{nlogn 最长上升子序列}
题意:求最长上升子序列,n=100000 思路:O(N^2)铁定超时啊....利用贪心的思想去找答案.利用栈,每次输入数据检查栈,二分查找替换掉最小比他大的数据,这样得到的栈就是更优的.这个题目确实不 ...
- poj 3903 Stock Exchange(最长上升子序列,模版题)
题目 #include<stdio.h> //最长上升子序列 nlogn //入口参数:数组名+数组长度,类型不限,结构体类型可以通过重载运算符实现 //数组下标从1号开始. int bs ...
- POJ 3903 Stock Exchange 【最长上升子序列】模板题
<题目链接> 题目大意: 裸的DP最长上升子序列,给你一段序列,求其最长上升子序列的长度,n^2的dp朴素算法过不了,这里用的是nlogn的算法,用了二分查找. O(nlogn)算法 #i ...
- POJ3903:Stock Exchange(LIS)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/E 题目: Description The world ...
随机推荐
- Swift 简单介绍 - 苹果最新的编程语言
Swift 真的能够说是最新的编程语言了,2014wwdc刚刚公布,以下来了解一下都有哪些特点. 首先感谢原作者,主要内容是借鉴他的,參考链接 http://zh.lucida.me/blog/an- ...
- Windows Phone开发(27):隔离存储A
原文:Windows Phone开发(27):隔离存储A 在很多资料或书籍上都翻译为"独立存储",不过,我想了一下,决定将IsolatedStorage翻译为"隔离存储& ...
- uva live 4394 String painter 间隔dp
// uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...
- SplitContainer如何实现左侧导航,正确显示和导航内容
关于这种类型的设计有很多的实现,这样,我首先解释一下我使用: 我的原则是实现方式,将form嵌panel在,作为一个子窗口. 如下面的代码细节: Form form = new DataSelect( ...
- 第1周 SQL Server 如何执行一个查询
原文:第1周 SQL Server 如何执行一个查询 大家好,欢迎来到第1周的SQL Server性能调优培训.在我们进入SQL Server性能调优里枯燥难懂的细节内容之前,我想通过讲解SQL Se ...
- 【iOS开发-60】案例学习:多组数据的tableView设置、添加右側组索引、多层数据模型设置以及valueForKeyPath
效果: 这里的数据模型有两层:每一组汽车是一层模型,每一组里面的每一行汽车品牌也是一层模型. (1)我们先创建一个WSCars模型. 在WSCars.h中: #import <Foundatio ...
- HTTP求
client联系server后,至server获取问题 Web 新闻资源,简称client至server发送一个 HTTP 求. 一个完整的 HTTP 该请求包含以下示例: ① ②若干消息头(请求头) ...
- oralce 获取自定义主键编码,有并发问题
F_GET_SEQUENCE,功能函数,当多个服务同时调用此函数,可能产生并发问题,待解决,加主键. CREATE OR REPLACE Function f_Get_Sequence(As_Comp ...
- ICT工作的思考<两>
2周奋战.我负责的LB昨天完成了最后一个模块.最后20日. 一周早于预期,经理说,出乎他的意料.So 奖励表,昨日,管理人员与我们合作,吃烧烤补补身子.我只想说,最后一个喘息. 这两周的生活确挺忙碌的 ...
- 【Java基础】“数三退一”问题的代码实现
现在有500个小孩,编号为0-499,手牵手连成一个圈,从第一个小孩开始"123123..."报数,数到三的小孩退出,求剩下的最后一个小孩的编号. public static vo ...