LIS(nlogn) POJ 3903 Stock Exchange
题意:LIS最长递增子序列 O(nlogn)
分析:设当前最长递增子序列为len,考虑元素a[i]; 若d[len]<a[i],则len++,并使d[len]=a[i]; 否则,在d[1~len]中二分查找第一个大于等于a[i]的位置j,使d[j]=a[i]。附上打印路径代码(准确性未知)
代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std; const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int a[N], d[N], pos[N], fa[N];
int n; void LIS(void) {
int len = 1; d[1] = a[1]; fa[1] = -1;
for (int i=2; i<=n; ++i) {
if (d[len] < a[i]) {
d[++len] = a[i];
// pos[len] = i; fa[i] = pos[len-1];
}
else {
int j = lower_bound (d+1, d+1+len, a[i]) - d;
d[j] = a[i];
// pos[j] = i; fa[i] = (j == 1) ? -1 : pos[j-1];
}
}
printf ("%d\n", len);
// vector<int> res; int i;
// for (i=pos[len]; ~fa[i]; i=fa[i]) res.push_back (a[i]);
// res.push_back (a[i]);
// for (int i=res.size ()-1; i>=0; --i) printf ("%d%c", res[i], i == 0 ? '\n' : ' ');
} int main(void) {
while (scanf ("%d", &n) == 1) {
for (int i=1; i<=n; ++i) {
scanf ("%d", &a[i]);
} LIS ();
} return 0;
}
LIS(nlogn) POJ 3903 Stock Exchange的更多相关文章
- 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(LIS)
一.Description The world financial crisis is quite a subject. Some people are more relaxed while othe ...
- POJ 3903 Stock Exchange
Stock Exchange Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2954 Accepted: 1082 De ...
- POJ 3903 Stock Exchange(LIS || 线段树)题解
题意:求最大上升子序列 思路:才发现自己不会LIS,用线段树写的,也没说数据范围就写了个离散化,每次查找以1~a[i]-1结尾的最大序列答案,然后更新,这样遍历一遍就行了.最近代码总是写残啊... 刚 ...
- {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 最长上升子序列入门题
题目链接:http://poj.org/problem?id=3903 最长上升子序列入门题. 算法时间复杂度 O(n*logn) . 代码: #include <iostream> #i ...
- POJ 3903 Stock Exchange 【最长上升子序列】模板题
<题目链接> 题目大意: 裸的DP最长上升子序列,给你一段序列,求其最长上升子序列的长度,n^2的dp朴素算法过不了,这里用的是nlogn的算法,用了二分查找. O(nlogn)算法 #i ...
随机推荐
- cocos基础教程(1)Mac环境下搭建
下面主要介绍cocos2d-x环境的设置以及android的环境搭建 1.下载cocos2d-x 3.0正式版 http://www.cocos2d-x.org/download 2.下载a ...
- OpenSwitch操作系统成为Linux基金会官方项目
导读 非盈利机构Linux基金会为推进Linux和开源软件在企业和专业人士的发展,于今天宣布OpenSwitch项目成为Linux基金会官方项目之一. Linux基金会的常务董事Jim Zemlin表 ...
- 纹理缓存(Texture Cache)
纹理缓存是将纹理缓存起来方便之后的绘制工作.每一个缓存的图像的大小,颜色和区域范围都是可以被修改的.这些信息都是存储在内存中的,不用在每一次绘制的时候都发送给GPU. CCTextureCache C ...
- 【js】将table的每个td的内容自动赋值给其title属性
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- centos 单独安装PHP的mysql和mysqli扩展
2013年11月22日 11:25:41 Linux centos 6.3 最小化安装 mysql 5.5 php 5.4 安装PHP时只是 ./configure --prefix=/**** 并没 ...
- Java for LeetCode 199 Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- Gym 100851G Generators (vector+鸽笼原理)
Problem G. Generators Input file: generators.in Output file: generators.outLittle Roman is studying li ...
- 在qq中可以使用添加标签功能
而在sina中不可以,现在就保持一致吧!那么每天使用的日志主要是记录工作项目上的问题还有生活的感受
- xampp 访问出现New XAMPP security concept
在浏览器输入 http://60.10.140.22/xampp出现以下错误信息: Access forbidden! New XAMPP security concept: Access to th ...
- 在MVC3或asp.net中修改KindEditor实现上传图片时添加水印
主要修改两个文件:image.js和upload_json.ashx文件. 一.修改image.js文件 打开kindeditor/plugins/image目录下的image.js文件,找到 '&l ...