POJ 3903 Stock Exchange(LIS || 线段树)题解
题意:求最大上升子序列
思路:才发现自己不会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 || 线段树)题解的更多相关文章
- 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 ...
- LIS(nlogn) POJ 3903 Stock Exchange
题目传送门 题意:LIS最长递增子序列 O(nlogn) 分析:设当前最长递增子序列为len,考虑元素a[i]; 若d[len]<a[i],则len++,并使d[len]=a[i]; 否则,在d ...
- POJ 3903 Stock Exchange 最长上升子序列入门题
题目链接:http://poj.org/problem?id=3903 最长上升子序列入门题. 算法时间复杂度 O(n*logn) . 代码: #include <iostream> #i ...
- poj 3903 Stock Exchange(最长上升子序列,模版题)
题目 #include<stdio.h> //最长上升子序列 nlogn //入口参数:数组名+数组长度,类型不限,结构体类型可以通过重载运算符实现 //数组下标从1号开始. int bs ...
- {POJ}{3903}{Stock Exchange}{nlogn 最长上升子序列}
题意:求最长上升子序列,n=100000 思路:O(N^2)铁定超时啊....利用贪心的思想去找答案.利用栈,每次输入数据检查栈,二分查找替换掉最小比他大的数据,这样得到的栈就是更优的.这个题目确实不 ...
- POJ 3903 Stock Exchange 【最长上升子序列】模板题
<题目链接> 题目大意: 裸的DP最长上升子序列,给你一段序列,求其最长上升子序列的长度,n^2的dp朴素算法过不了,这里用的是nlogn的算法,用了二分查找. O(nlogn)算法 #i ...
随机推荐
- memory consistency
目前的计算机系统中,都是shared memory结构,提供统一的控制接口给软件, shared memory结构中,为了memory correctness,可以将问题分为:memory consi ...
- Vue系列之 => 列表动画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java多线程-----线程池详解
1. 线程池的实现原理 提交一个任务到线程池中,线程池的处理流程如下: 判断线程池里的核心线程是否都在执行任务,如果不是(核心线程空闲或者还有核心线程没有被创建)则创建一个新的工作线程来执行任务.如果 ...
- Hive分区表新增字段及修改表名,列名,列注释,表注释,增加列,调整列顺序,属性名等操作
一.Hive分区表新增字段 参考博客:https://blog.csdn.net/yeweiouyang/article/details/44851459 二.Hive修改表名,列名,列注释,表注释, ...
- DataX介绍
一. DataX3.0概览 DataX 是一个异构数据源离线同步工具,致力于实现包括关系型数据库(MySQL.Oracle等).HDFS.Hive.ODPS.HBase.FTP等各种异构数据源之间稳定 ...
- centos 6.8操作系统安装arcgis server 10.4
1.检查操作系统中软件包的安装,第一条和第二条是图形界面工具,可以不装. 可以用rpm -qa | grep 软件名 命令检查软件包是否已经安装 主机名不能包含下划线,可以用hostname检查主 ...
- [转载]SQL Server中的事务与锁
了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂,不能保证数据的安全正确读写. 死锁: ...
- Linux 用户管理【UID和GID】
Linux 用户和用户组管理 Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统. 用户的账号一方面可以帮助 ...
- win10 +python3.6环境下安装opencv以及pycharm导入cv2有问题的解决办法
一.安装opencv 借鉴的这篇博客已经写得很清楚了--------https://blog.csdn.net/u011321546/article/details/79499598 ,这 ...
- django项目----函数和方法的区别
一.函数和方法的区别 1.函数要手动传self,方法不用传 2.如果是一个函数,用类名去调用,如果是一个方法,用对象去调用 举例说明: class Foo(object): def __init__( ...