题目1533:最长上升子序列 (nlogn | 树状数组)
题目1533:最长上升子序列
http://ac.jobdu.com/problem.php?pid=1533
时间限制:1 秒
内存限制:128 兆
特殊判题:否
提交:857
解决:178
- 题目描述:
-
给定一个整型数组, 求这个数组的最长严格递增子序列的长度。 譬如序列1 2 2 4 3 的最长严格递增子序列为1,2,4或1,2,3.他们的长度为3。
- 输入:
-
输入可能包含多个测试案例。
对于每个测试案例,输入的第一行为一个整数n(1<=n<=100000):代表将要输入的序列长度
输入的第二行包括n个整数,代表这个数组中的数字。整数均在int范围内。
- 输出:
-
对于每个测试案例,输出其最长严格递增子序列长度。
- 样例输入:
-
4
4 2 1 3
5
1 1 1 1 1
- 样例输出:
-
2
1#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector> using namespace std; const int N=; int n,val[N];
vector<int> vt; int binarySearch(int x){
int left=,right=vt.size()-;
int mid;
while(left<=right){
mid=(left+right)>>;
if(vt[mid]<x)
left=mid+;
else
right=mid-;
}
return left;
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n)){
vt.clear();
for(int i=;i<n;i++)
scanf("%d",&val[i]);
int tmp;
for(int i=;i<n;i++){
tmp=binarySearch(val[i]);
if(tmp>=(int)vt.size())
vt.push_back(val[i]);
else
vt[tmp]=val[i];
}
int ans=vt.size();
printf("%d\n",ans);
}
return ;
}#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int N=; int n,val[N],a[N];
int len,arr[N]; int lowbit(int x){
return x&(-x);
} void update(int i,int x){
while(x<=len){
if(i>arr[x])
arr[x]=i;
x+=lowbit(x);
}
} int query(int x){
int ans=;
while(x){
if(arr[x]>ans)
ans=arr[x];
x-=lowbit(x);
}
return ans;
} int main(){ freopen("input.txt","r",stdin); while(~scanf("%d",&n)){
for(int i=;i<n;i++){
scanf("%d",&val[i]);
a[i]=val[i];
}
sort(a,a+n);
len=unique(a,a+n)-a;
memset(arr,,sizeof(arr));
int ans=,tmp;
for(int i=;i<n;i++){
val[i]=lower_bound(a,a+len,val[i])-a+;
tmp=query(val[i]-)+;
if(tmp>ans)
ans=tmp;
update(tmp,val[i]);
}
printf("%d\n",ans);
}
return ;
}
题目1533:最长上升子序列 (nlogn | 树状数组)的更多相关文章
- [poj 1533]最长上升子序列nlogn树状数组
题目链接:http://poj.org/problem?id=2533 其实这个题的数据范围n^2都可以过,只是为了练习一下nlogn的写法. 最长上升子序列的nlogn写法有两种,一种是变形的dp, ...
- 【BZOJ】3173: [Tjoi2013]最长上升子序列(树状数组)
[题意]给定ai,将1~n从小到大插入到第ai个数字之后,求每次插入后的LIS长度. [算法]树状数组||平衡树 [题解] 这是树状数组的一个用法:O(n log n)寻找前缀和为k的最小位置.(当数 ...
- [bzoj2124]等差子序列(hash+树状数组)
我又来更博啦 2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 941 Solved: 348[Submit][Statu ...
- BZOJ2124 等差子序列(树状数组+哈希)
容易想到一种暴力的做法:枚举中间的位置,设该位置权值为x,如果其两边存在权值关于x对称即合法. 问题是如何快速寻找这个东西是否存在.考虑仅将该位置左边出现的权值标1.那么若在值域上若关于x对称的两权值 ...
- BZOJ5157 [Tjoi2014]上升子序列 【树状数组】
题目链接 BZOJ5157 题解 我们只需计算每个位置为开头产生的贡献大小,就相当于之后每个大于当前位置的位置产生的贡献 + 1之和 离散化后用树状数组维护即可 要注意去重,后面计算的包含之前的,记录 ...
- bzoj2124 等差子序列(树状数组+hash)
题意 给你一个1~n排列,问有没有一个等差数列(长度至少为3) 题解 我居然自己想到了正解. 但我最后写挂了,所以我又看了题解. 我们维护了一个以权值为下标的01序列. 我们扫描整个序列.对于每一个正 ...
- BZOJ2124: 等差子序列(树状数组&hash -> bitset 求是否存在长度为3的等差数列)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 2354 Solved: 826[Submit][Status][Discuss ...
- bzoj 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛【dp+树状数组+hash】
最长上升子序列.虽然数据可以直接n方但是另写了个nlogn的 转移:f[i]=max(f[j]+1)(a[j]<a[i]) O(n^2) #include<iostream> #in ...
- Ultra-QuickSort---poj2299 (归并排序.逆序数.树状数组.离散化)
题目链接:http://poj.org/problem?id=2299 题意就是求把数组按从小到大的顺序排列,每次只能交换相邻的两个数, 求至少交换了几次 就是求逆序数 #include<std ...
随机推荐
- JavaScript-创建新数组
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- HDU 1165 Eddy's research II (找规律)
题意:给定一个表达式,然后让你求表达式的值. 析:多写几个就会发现规律. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400000 ...
- 文件和目录:access函数
access函数是按照实际用户ID和实际组ID进行访问权限测试的: #include <unistd.h> int access( const char *pathname, int mo ...
- submit(提交)按钮
为form添加一个submit(提交)按钮,点击这个按钮,表单中的数据将会被发送到通过action属性指定的地址上. 下面是submit按钮的例子: <button type="sub ...
- [Leetcode][JAVA] Triangled
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- day4----生成器,迭代器
迭代器,生成器,装饰器 1.生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要 ...
- halcon摄像机标定
摄像机标定程序: 注意:E:/calibration_image :为标定图像文件路径 'E:/calibration_description/caltab_123mm.descr:为标定 ...
- 轻量级模块化开发框架 Hasor 核心模块 v0.0.2 发布
首先引用Wiki的介绍一下Hasor: “Hasor是一款开源框架.它是为了解决企业模块化开发中复杂性而创建的.Hasor遵循简单的依赖.单一职责,在开发多模块企业项目中更加有调理.然 而Ha ...
- Designing a Secure REST (Web) API without OAuth
原文:http://www.thebuzzmedia.com/designing-a-secure-rest-api-without-oauth-authentication/ Situation Y ...
- Xperf Analysis Basics(转)
FQ不易,转载 http://randomascii.wordpress.com/2011/08/23/xperf-analysis-basics/ I started writing a des ...