题意:求最大上升子序列

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

刚看了LIS的nlogn写法(贪心+二分):维护一个dp[i]表示最大长度为i时的最小结尾,初始memset为INF,最终dp数组的长度为答案。这个很好维护,如果当前的a[i]比dp[len]要大,那么显然最大长度加一,dp[len + 1] = a[i];如果比dp[len]小,那么我就去二分查找前面的第一个dp[x]大于等于a[i],替换掉,因为长度x的结尾越小越好。

LIS代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 1e5 + ;
const int seed = ;
const ll MOD = ;
const int INF = 0x3f3f3f3f;
int a[maxn], dp[maxn];
int main(){
int n;
while(~scanf("%d", &n)){
memset(dp, INF, sizeof(dp));
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
dp[] = a[];
int len = ;
for(int i = ; i <= n; i++){
if(a[i] > dp[len]){
dp[++len] = a[i];
}
else{
int pos = lower_bound(dp + , dp + len + , a[i]) - dp;
if(pos != len + ){
dp[pos] = a[i];
}
}
}
printf("%d\n", len);
}
return ;
}

线段树代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 1e5 + ;
const int seed = ;
const ll MOD = ;
const int INF = 0x3f3f3f3f;
ll a[maxn], b[maxn];
ll Max[maxn << ];
void update(int l, int r, int pos, ll v, int rt){
if(l == r){
Max[rt] = max(Max[rt], v);
return;
}
int m = (l + r) >> ;
if(pos <= m)
update(l, m, pos, v, rt << );
else
update(m + , r, pos, v, rt << | );
Max[rt] = max(Max[rt << ], Max[rt << | ]);
}
int query(int l, int r, int L, int R, int rt){
if(R < ) return ;
if(L <= l && R >= r){
return Max[rt];
}
int m = (l + r) >> , ans = ;
if(L <= m)
ans = max(ans, query(l, m, L, R, rt << ));
if(R > m)
ans = max(ans, query(m + , r, L, R, rt << | ));
return ans;
}
int main(){
int n;
while(~scanf("%d", &n)){
memset(Max, , sizeof(Max));
for(int i = ; i <= n; i++)
scanf("%lld", &a[i]), b[i] = a[i];
sort(b + , b + n + );
for(int i = ; i <= n; i++)
a[i] = lower_bound(b + , b + n + , a[i]) - b;
int ans = ;
for(int i = ; i <= n; i++){
int temp = query(, n, , a[i] - , ) + ;
ans = max(ans, temp);
update(, n, a[i], temp, );
}
printf("%d\n", ans);
}
return ;
}
/*
10
1 5 2 7 5 9 10 465 10 78
*/

POJ 3903 Stock Exchange(LIS || 线段树)题解的更多相关文章

  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. LIS(nlogn) POJ 3903 Stock Exchange

    题目传送门 题意:LIS最长递增子序列 O(nlogn) 分析:设当前最长递增子序列为len,考虑元素a[i]; 若d[len]<a[i],则len++,并使d[len]=a[i]; 否则,在d ...

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

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

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

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

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

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

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

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

随机推荐

  1. Oracle使用rman备份数据库时出现cannot reclaim的错误

    1. 按照<2 day DBA>中的guide,设置fast recovery area. SQL> ALTER SYSTEM SET DB_RECOVERY_FILE_DEST_S ...

  2. JSP FreeMarker Velocity 原理

    JSP原理 JSP的运行原理:JSP 本质上是一个Servlet. 每个JSP 页面在第一次被访问时,JSP引擎将它翻译成一个Servlet 程序,然后再把这个 Servlet 源程序编译成Servl ...

  3. bash shell 编程练习

    原始文件: find /etc -name passwd 2>&1 | tee ee.log 1. cat -n 把 e.log 的文档内容加上行号后输入 e2.log 这个文档里: x ...

  4. 直流-直流(DC-DC)变换电路_BUCK&BOOST变换电路

    1. 直流—直流变换器通过对电力电子器件的通断控制,将直流电压断续地加到负载上,通过改变占空比改变输出电压平均值. BUCK线路原理图如上,其中Q管/MOS作为开关管,驱动电压一般为PWM. 当开关管 ...

  5. 关于SqlCommand对象的2个方法:ExecuteNonQuery 方法和ExecuteScalar方法

    1.SqlCommand.ExecuteNonQuery 方法 对连接执行 Transact-SQL 语句并返回受影响的行数. 语法:public override int ExecuteNonQue ...

  6. Fabric架构:抽象的逻辑架构与实际的运行时架构

    Fabric从1.X开始,在扩展性及安全性上面有了大大的提升,且新增了诸多的新特性: 多通道:支持多通道,提高隔离安全性. 可拔插的组件:支持共识组件.权限管理组件等可拔插功能. 账本数据可被存储为多 ...

  7. go语言,golang学习笔记3 用命令下载框架报错问题解决 设置环境变量

    go语言,golang学习笔记3 用命令下载框架报错问题解决 设置环境变量 下载安装:go get github.com/astaxie/beego 首页 - beego: 简约 & 强大并存 ...

  8. php Allocator Jemalloc TCMalloc那个内存分配器比较好?

    php Allocator Jemalloc TCMalloc那个内存分配器比较好? php一键安装脚本可以选择是否安装内存优化 You have 3 options for your Memory ...

  9. 算法提高 P0102

    用户输入三个字符,每个字符取值范围是0-9,A-F.然后程序会把这三个字符转化为相应的十六进制整数,并分别以十六进制,十进制,八进制输出,十六进制表示成3位,八进制表示成4位,若不够前面补0.(不考虑 ...

  10. AtCoder Beginner Contest 045 C - たくさんの数式 / Many Formulas

    Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement You are given a string ...