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 ...
随机推荐
- 记前些日子archlinux更新后无法调节声音的解决方法
桌面环境用的是xfce4. 自从某次更新过后,panel中调节声音的插件变成了 xfce4-pulseaudio-plugin.然后就发现在panel中无法调节声音了. 在这个插件的属性中发现了一项设 ...
- Mockito/PowerMockito Straige Issues
http://blog.csdn.net/xiaoyaoyulinger/article/details/52415494 http://breezylee.iteye.com/blog/208843 ...
- map 的用法
#include<iostream> #include<map> #include<string> #define s second #define f first ...
- 如何注销Sitecore CMS
登录Sitecore很容易,但是在旧版本的Sitecore中使用不同的界面,退出可能会给未经证实的人带来挑战. Sitecore 8 Sitecore 6和7 Sitecore 8 Sitecore ...
- pymysql 数据库操控
模块安装 pip install pymysql 执行sql语句 import pymysql #添加数据 conn = pymysql.connect(host='127.0.0.1', port= ...
- 【Linux学习四】正则表达式
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 一.grep:显示匹配行v:反显示e:使用扩展正则表达式E:不使用正则 ...
- 基于word2vec训练词向量(二)
转自:http://www.tensorflownews.com/2018/04/19/word2vec2/ 一.基于Hierarchical Softmax的word2vec模型的缺点 上篇说了Hi ...
- python内置函数bin
bin() 返回一个整数 int 或者长整数 long int 的二进制表示. 代码示例: print(bin(10)) 运行结果: F:\dev\python\python.exe F:/pyCha ...
- appium随笔
目录结构如下: Test_edaike---page object设计思想 定位元素和脚本分离Images目录---用例失败截图case目录 eTestfastfood.py---定位app界面元素& ...
- NoSql Cassandra
我们为什么要使用NOSQL非关系数据库? 随着互联网web2.0网站的兴起,非关系型的数据库现在成了一个极其热门的新领域,非关系数据库产品的发展非常迅速.而传统的关系数据库在应付web2.0网站,特别 ...