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 ...
随机推荐
- 矩阵快速幂---BestCoder Round#8 1002
当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?可以用矩阵快速幂来加速计算.我们可以用矩阵来表示数列递推公式比如fibonacci数列 可以表示为 [f(n) f(n-1)] = [f(n ...
- Foursquare 8.0 :聪明人给互联网公司上的流量转化课
今年 5 月上线的 Swarm 虽然应用制作精良,但不免让人怀疑是 Foursquare一次失败的互联网公司服务越界和用户忠诚度试水.但非常快这群聪明人让我们发现事情并没有这么简单:他们给互联网公司们 ...
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(四)
全系列目录: [原创]shadowebdict开发日记:基于linux的简明英汉字典(一) [原创]shadowebdict开发日记:基于linux的简明英汉字典(二) [原创]shadowebdic ...
- Android 4.0新组件:GridLayout详细说明
于Android 4.0(API 14)它提供了一个新的组件GridLayout,它继承自Linearlayout,用于执行网络格样式布局. 在某些方面,GridLayout与TableLayout和 ...
- VMware vSphere 服务器虚拟化之十六 桌面虚拟化之VMware Horizon View
VMware vSphere服务器虚拟化之十六 桌面虚拟化之VMware Horizon View VMware Horizon View (原VMware View的升级版现在版本5.2)是 ...
- Nubia Z5S 官方4.4 201内測版 内核版本号信息
从egl推断内核的的版本号: OpenGL ES Shader Compiler Version: E031.24.00.14 Build Date: 04/29/14 Tue Local Branc ...
- Git相关操作汇总
git clone: 正如上图,当我们打开终端的情况下,默认我们所在的目录是在/home/shiyanlou的,大家可以在终端输入以下命令把目录切换到桌面cd /home/Desktop这个时候输入 ...
- 直接插入排序---java实现
思路:遍历无序的原数组,把第i个的后一个即i+1去与前面的i个逐个比较... 解法一: package com.sheepmu.text; import java.util.Arrays; /* * ...
- 杭电1171 Big Event in HDU(母函数+多重背包解法)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- phonegap+emberjs+python手机店发展,html5实现本地车类别~
商城开发项目,现在需要做出APP,无奈出场前android但不是很精通.最后选择phonegap实现app. 由于之前办理购物车分为登陆和登陆后两种情况,登录前必须充分利用本地存储.而基于phoneg ...