题目传送门

题意: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的更多相关文章

  1. POJ 3903 Stock Exchange (E - LIS 最长上升子序列)

    POJ 3903    Stock Exchange  (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. POJ - 3903 Stock Exchange(LIS最长上升子序列问题)

    E - LIS Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descripti ...

  3. Poj 3903 Stock Exchange(LIS)

    一.Description The world financial crisis is quite a subject. Some people are more relaxed while othe ...

  4. POJ 3903 Stock Exchange

    Stock Exchange Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2954   Accepted: 1082 De ...

  5. POJ 3903 Stock Exchange(LIS || 线段树)题解

    题意:求最大上升子序列 思路:才发现自己不会LIS,用线段树写的,也没说数据范围就写了个离散化,每次查找以1~a[i]-1结尾的最大序列答案,然后更新,这样遍历一遍就行了.最近代码总是写残啊... 刚 ...

  6. {POJ}{3903}{Stock Exchange}{nlogn 最长上升子序列}

    题意:求最长上升子序列,n=100000 思路:O(N^2)铁定超时啊....利用贪心的思想去找答案.利用栈,每次输入数据检查栈,二分查找替换掉最小比他大的数据,这样得到的栈就是更优的.这个题目确实不 ...

  7. poj 3903 Stock Exchange(最长上升子序列,模版题)

    题目 #include<stdio.h> //最长上升子序列 nlogn //入口参数:数组名+数组长度,类型不限,结构体类型可以通过重载运算符实现 //数组下标从1号开始. int bs ...

  8. POJ 3903 Stock Exchange 最长上升子序列入门题

    题目链接:http://poj.org/problem?id=3903 最长上升子序列入门题. 算法时间复杂度 O(n*logn) . 代码: #include <iostream> #i ...

  9. POJ 3903 Stock Exchange 【最长上升子序列】模板题

    <题目链接> 题目大意: 裸的DP最长上升子序列,给你一段序列,求其最长上升子序列的长度,n^2的dp朴素算法过不了,这里用的是nlogn的算法,用了二分查找. O(nlogn)算法 #i ...

随机推荐

  1. poj.2419.Forests (枚举 + set用法)

    Forests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5782   Accepted: 2218 Descripti ...

  2. cocos基础教程(8)粒子效果

    简介 粒子系统是指计算机图形学中模拟特定现象的技术,它在模仿自然现象.物理现象及空间扭曲上具备得天独厚的优势,为我们实现一些真实自然而又带有随机性的特效(如爆炸.烟花.水流)提供了方便. 粒子属性 一 ...

  3. /run/systemd/private: No such file or directory

    今天执行consul脚本的时候报错 /run/systemd/private: No such file or directory reboot -f 重启电脑private文件就出现了.

  4. HDU1198水管并查集Farm Irrigation

    Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...

  5. ssh连接慢的问题的解决?

    <1>群中同学遇到的问题,我之前在uuwatch也遇到了同样的问题? 问个问题师兄们 突然之间 公司服务器连接很慢 连一个shell需要10几秒钟 服务器就在公司全是内网服务器, 我也不知 ...

  6. MySQL报错:Packets larger than max_allowed_packet are not allowed 的解决方案

    在导大容量数据特别是CLOB数据时,可能会出现异常:“Packets larger than max_allowed_packet are not allowed”. 这是由于MySQL数据库有一个系 ...

  7. Jump Game | & ||

    Jump Game | Given an array of non-negative integers, you are initially positioned at the first index ...

  8. 22.整数二进制表示中1的个数[Get1BitCount]

    [题目] 输入一个整数,求该整数的二进制表达中有多少个1.例如输入10,由于其二进制表示为1010,有两个1,因此输出2. [分析] 如果一个整数不为0,那么这个整数至少有一位是1.如果我们把这个整数 ...

  9. Window环境下Python和Django的安装

    转载地址:http://blog.csdn.net/haoni123321/article/details/7593821 1.下载python,本文使用python-2.7.2.msi 2.下载dj ...

  10. icon上添加数字提醒

    使用viewbadger包: package com.jingle.vierbagerstudy; import android.app.Activity; import android.os.Bun ...