题目传送门

题意: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. Linux的防火墙--IP Tables

    导读 IP Table已经集成在Linux 2.4及以上版本的内核中,同Windows下的众多“傻瓜”防火墙不同的是,IP Table需要用户自己定制相关规则.下面我就给大家简单介绍一下关于防火墙的基 ...

  2. tomcat配置文件之Server.xml

    Server.xml包含的元素有<Server>.<Service>.<Connector>.<Engine>.<Host>.<Con ...

  3. Python字符串与数字互转,数字格式化

    # -*- coding: gbk -*- import re #将数字格式化为带三位数逗号的字符串 def formatNumber(number): numStr='%d'%number form ...

  4. 【Hibernate】Hibernate系列4之配置文件详解

    映射文件详解 4.1.概述 4.2.主键生成策略 4.3.属性配置 准确映射: 4.4.映射组成关系 4.5.单向多对一映射 4.6.双向多对一关系 4.7.一对一关联关系-基于外键映射 一对一联合m ...

  5. HDFS 原理、架构与特性介绍--转载

    原文地址:http://www.uml.org.cn/sjjm/201309044.asp 本文主要讲述 HDFS原理-架构.副本机制.HDFS负载均衡.机架感知.健壮性.文件删除恢复机制 1:当前H ...

  6. Java for LeetCode 048 Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  7. Java for LeetCode 045 Jump Game II

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

  8. Cocos2d-JS坐标系统

    标准屏幕坐标系 如果接触过iOS,Android,Windows Phone等系统的应用开发,或使用DOM,CSS开发过Web网页,开发者会非常熟悉所谓的标准屏幕坐标系:左上角为原点,向右为X轴正方向 ...

  9. win7下IIS安装与配置运行网站

    1.打开控制面板,点击程序和功能: 2.点击打开或关闭Windows功能进行安装: 3.等待进入安装界面,需要几十秒左右: 4.找到Internet信息服务,将Web管理工具和万维网服务所有勾上,然后 ...

  10. mysql 数据库获取当前时间

    mysql> select now(); +---------------------+ | now() | +---------------------+ | 2016-05-27 17:34 ...