POJ 3903 Stock Exchange 【最长上升子序列】模板题
<题目链接>
题目大意:
裸的DP最长上升子序列,给你一段序列,求其最长上升子序列的长度,n^2的dp朴素算法过不了,这里用的是nlogn的算法,用了二分查找。
O(nlogn)算法
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N= 1e5+;
int a[N],rise[N]; int main(){
int n;while(~scanf("%d",&n)){
memset(rise,,sizeof(rise));
for(int i=;i<n;i++)scanf("%d",&a[i]);
int len=;
rise[]=-1e9;
for(int i=;i<n;i++){
if(a[i]>rise[len])rise[++len]=a[i];
else{
int j=lower_bound(rise+,rise++len,a[i])-rise;
rise[j]=a[i];
}
}
printf("%d\n",len);
}
}
虽然(n^2)算法过不了此题,但是还是先记录下
#include<cstdio>
int main()
{
int i, j, n;
int dp[], a[];
while (scanf("%d", &n) != EOF)
{
int max = ;
for (i = ; i<n; i++)
scanf("%d", &a[i]);
dp[] = ;
for (i = ; i<n; i++)
{
dp[i] = ;
for (j = ; j<i; j++)
if (a[j]<a[i] && dp[j] + >dp[i])
dp[i] = dp[j] + ;
}
for (i = ; i<n; i++)
if (max<dp[i])
max = dp[i];
printf("%d\n", max);
}
}
POJ 3903 Stock Exchange 【最长上升子序列】模板题的更多相关文章
- POJ 3903 Stock Exchange 最长上升子序列入门题
题目链接:http://poj.org/problem?id=3903 最长上升子序列入门题. 算法时间复杂度 O(n*logn) . 代码: #include <iostream> #i ...
- 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(LIS最长上升子序列问题)
E - LIS Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descripti ...
- {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
Stock Exchange Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2954 Accepted: 1082 De ...
- Poj 3903 Stock Exchange(LIS)
一.Description The world financial crisis is quite a subject. Some people are more relaxed while othe ...
- LIS(nlogn) POJ 3903 Stock Exchange
题目传送门 题意:LIS最长递增子序列 O(nlogn) 分析:设当前最长递增子序列为len,考虑元素a[i]; 若d[len]<a[i],则len++,并使d[len]=a[i]; 否则,在d ...
- POJ-1458(LCS:最长公共子序列模板题)
Common Subsequence POJ-1458 //最长公共子序列问题 #include<iostream> #include<algorithm> #include& ...
随机推荐
- luogu P3576 [POI2014]MRO-Ant colony
传送门 一群蚂蚁能被吃,也就是走到指定边的两端点之一要走到另一端点时有\(k\)只,我们可以从这两端点逆推,记两个值为走到某个点时最后会被吃掉\(k\)只蚂蚁的蚂蚁数量范围,式子下面有,很好理解(雾) ...
- 第16月第24天 find iconv sublime utf-8
1. find . -type f -exec echo {} \; find src -type f -exec sh -c "iconv -f GB18030 -t UTF8 {} &g ...
- JavaScript学习 - 基础(二) - 基础类型/类型转换
基础类型 - 数字类型(Number) 1.最基本的数据类型 2.不区分整型数值和浮点型数值 3.所有数字采用64位浮点格式存储,相当于Java和C语言中double格式 4.能表示的最大值 +- 1 ...
- More Effective C++ 条款0,1
More Effective C++ 条款0,1 条款0 关于编译器 不同的编译器支持C++的特性能力不同.有些编译器不支持bool类型,此时可用 enum bool{false, true};枚举类 ...
- [转]python 装饰器
以前你有没有这样一段经历:很久之前你写过一个函数,现在你突然有了个想法就是你想看看,以前那个函数在你数据集上的运行时间是多少,这时候你可以修改之前代码为它加上计时的功能,但是这样的话是不是还要大体读读 ...
- JS直接if参数的用法
经常在JS中见一些代码直接if(参数),然后参数调用的时候是将元素自己传下去.例如下面代码: <body> <input type="text" name=&qu ...
- VIM for C++ 一键安装配置
执行: wget https://raw.github.com/ma6174/vim/master/setup.sh -O ma6174_vim_setup.sh && bash ma ...
- C++获取当前所有进程的完整路径
实现代码 #include <stdio.h> #include <windows.h> #include <tlhelp32.h> #include <st ...
- Vistual Studio Community 2017 账号的许可证过期,公安网激活方法
方法: 1.外网电脑打开Vistual Studio Community2017 2.在许可证过期弹窗中登陆账号即可自动下载许可证完成激活 许可证下载路径(C:\用户\user\Ap ...
- Spring的Aspect切面类不能拦截Controller中的方法
根本原因在于<aop:aspectj-autoproxy />这句话是在spring的配置文件内,还是在springmvc的配置文件内.如果是在spring的配置文件内,则@Control ...