题意:求最大上升子序列

思路:才发现自己不会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. sql server case

    use mytest go exec p_city 2,4 exec p_city_cnt 2,3 select stuff((select ',' + city_id from cities for ...

  2. <<Natural Language Inference over Interaction Space >> 句子匹配

    模型结构 code :https://github.com/YichenGong/Densely-Interactive-Inference-Network 首先是模型图: Embedding Lay ...

  3. Nginx技术研究系列6-配置详解

    前两篇文章介绍了Nginx反向代理和动态路由: Ngnix技术研究系列1-通过应用场景看Nginx的反向代理 Ngnix技术研究系列2-基于Redis实现动态路由 随着研究的深入,很重要的一点就是了解 ...

  4. uvalive 5731 Qin Shi Huang’s National Road System

    题意: 秦始皇要修路使得所有的城市连起来,并且花费最少:有一个人,叫徐福,他可以修一条魔法路,不花费任何的钱与劳动力. 秦始皇想让修路的费用最少,但是徐福想要受益的人最多,所以他们经过协商,决定让 A ...

  5. 2017-2018-1 20155228 《信息安全系统设计基础》第六周学习总结&课下作业

    20155228 2017-2018-1 <信息安全系统设计基础>第六周学习总结&课下作业 教材学习内容总结 异常及其种类 异常可以分为四类:中断(interrupt) ,陷阱(t ...

  6. 反射(I)

    反射获取属性和属性值 let item = DoctorGroup() guard let dic = InterfaceTests.obtainValues(subObject: item) els ...

  7. 宏和函数的区别(一个BUG的总结)

    [1]BUG简单示例代码 主要代码如下: #include <QCoreApplication> #include <QDebug> #define ADD(a, b, c) ...

  8. 使用SpringAOP获取一次请求流经方法的调用次数和调用耗时

    引语 作为工程师,不能仅仅满足于实现了现有的功能逻辑,还必须深入认识系统.一次请求,流经了哪些方法,执行了多少次DB操作,访问了多少次文件操作,调用多少次API操作,总共有多少次IO操作,多少CPU操 ...

  9. python小练习:读入一个考试得分,判断这个分数是哪个等级,并输出,考虑异常场景

    读入一个考试得分,判断这个分数是哪个等级,并输出. 等级:>=90 优 ,>=80且小于90 良,>=70 且小于80,中,>=60且<70及格  <60 不及格 ...

  10. php CI框架中URL特殊字符处理与SQL注入隐患

    php CI框架中URL特殊字符处理与SQL注入隐患 php CI框架中URL特殊字符有很多是不支持的,导致像c++,括号这些常用的分类,字符都无法正常显示很头痛,而在配置里增加单引号' 反斜杠\ 这 ...