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 ...
随机推荐
- 学习笔记 Android.mk 搜索自己主动
最近一直Android.mk这是什么一个令人沮丧的夜晚,点击此处记录. ios你担心更多.不管那么多.xcode自己解决. 文本工具:MACVIM(文本编辑工具 很有用 你可以清楚地分辨tab 和Sp ...
- poj 2369 Permutations 更换水称号
寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...
- WCF异常传播
传送至客户端的异常肯定是CommunitionException类型,包括一般的通信过程中出错而引发的CommunicationException类型,System.IdentityModel.Sel ...
- Shiro学习笔记(5)——web集成
Web集成 shiro配置文件shiroini 界面 webxml最关键 Servlet 測试 基于 Basic 的拦截器身份验证 Web集成 大多数情况.web项目都会集成spring.shiro在 ...
- Three.js 3D打印数据模型文件(.STL)载入中
3DPrint是现在和未来10年度科技产品的主流之中.广泛的. 对于电子商务类3D打印网站.一个主要功能就是商品3D呈现的方式,那是,3D数据可视化技术. HTML5(WebGL)它可以用于构建3D查 ...
- HSQL
Whenever I connect to HSQLDB from my application deployed on eclipse Juno, it throws an exception as ...
- Codeforces Round #257 (Div. 2) 题解
Problem A A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes inp ...
- ViewPager空指针错误,android.support.v4.view.ViewPager.onSaveInstanceState
support.v4 包为我们提供了一个非常实用的滑动控件ViewPager,在使用ViewPager时有一个需要注意的地方: 即: android.support.v4.view.ViewPager ...
- Prototype Pattern 原型模式
7.6 原型模式总结 原型模式作为一种快速创建大量相同或相似对象的方式,在软件开发中应用较为广泛,很多软件提供的复制(Ctrl + C)和粘贴(Ctrl + V)操作就是原型模式的典型应用,下面对该模 ...
- Springmvc +JNDI 在Tomcat下 配置数据源(转)
一. 简介 jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务 ...