题目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 | 树状数组)的更多相关文章

  1. [poj 1533]最长上升子序列nlogn树状数组

    题目链接:http://poj.org/problem?id=2533 其实这个题的数据范围n^2都可以过,只是为了练习一下nlogn的写法. 最长上升子序列的nlogn写法有两种,一种是变形的dp, ...

  2. 【BZOJ】3173: [Tjoi2013]最长上升子序列(树状数组)

    [题意]给定ai,将1~n从小到大插入到第ai个数字之后,求每次插入后的LIS长度. [算法]树状数组||平衡树 [题解] 这是树状数组的一个用法:O(n log n)寻找前缀和为k的最小位置.(当数 ...

  3. [bzoj2124]等差子序列(hash+树状数组)

    我又来更博啦     2124: 等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 941  Solved: 348[Submit][Statu ...

  4. BZOJ2124 等差子序列(树状数组+哈希)

    容易想到一种暴力的做法:枚举中间的位置,设该位置权值为x,如果其两边存在权值关于x对称即合法. 问题是如何快速寻找这个东西是否存在.考虑仅将该位置左边出现的权值标1.那么若在值域上若关于x对称的两权值 ...

  5. BZOJ5157 [Tjoi2014]上升子序列 【树状数组】

    题目链接 BZOJ5157 题解 我们只需计算每个位置为开头产生的贡献大小,就相当于之后每个大于当前位置的位置产生的贡献 + 1之和 离散化后用树状数组维护即可 要注意去重,后面计算的包含之前的,记录 ...

  6. bzoj2124 等差子序列(树状数组+hash)

    题意 给你一个1~n排列,问有没有一个等差数列(长度至少为3) 题解 我居然自己想到了正解. 但我最后写挂了,所以我又看了题解. 我们维护了一个以权值为下标的01序列. 我们扫描整个序列.对于每一个正 ...

  7. BZOJ2124: 等差子序列(树状数组&hash -> bitset 求是否存在长度为3的等差数列)

    2124: 等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 2354  Solved: 826[Submit][Status][Discuss ...

  8. 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 ...

  9. Ultra-QuickSort---poj2299 (归并排序.逆序数.树状数组.离散化)

    题目链接:http://poj.org/problem?id=2299 题意就是求把数组按从小到大的顺序排列,每次只能交换相邻的两个数, 求至少交换了几次 就是求逆序数 #include<std ...

随机推荐

  1. Spring AOP小结

    一. AOP介绍 AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP ...

  2. 【堆】【kd-tree】bzoj2626 JZPFAR

    用堆记录答案.看看当前点是否比堆顶更优. #include<cstdio> #include<queue> #include<cstring> #include&l ...

  3. iOS设置UISearchBar光标的颜色

    [[UISearchBar appearance] setTintColor:[UIColor blackColor]];

  4. python 常用函数(不定时更新)

    1.遍历文件夹 import os def FileList(rootDir): FL=[] for lists in os.listdir(rootDir): path=os.path.join(r ...

  5. 《Diagnostic use of facial image analysis software in endocrine and genetic disorders: review, current results and future perspectives》学习笔记

    <使用面部图像分析软件诊断内分泌和遗传疾病:回顾,当前研究结果以及未来展望> Abstract 库欣综合征(CS)和肢端肥大症普遍是在发病后几年才能被诊断出的内分泌疾病.现在需要新的诊断方 ...

  6. Nginx提示502和504错误的解决方案

    一.错误提示说明: Nginx 502 Bad Gateway的含义是请求的PHP-CGI已经执行,但是由于某种原因(一般是读取资源的问题)没有执行完毕而导致PHP-CGI进程终止. Nginx 50 ...

  7. 【原创】“借贷宝”砸钱,邀请码 GZZKZK2 (注册成功每人可得20现金,可直接提现)。。。而这只是开始

    作为IT/互联网资深码农的我,从专业技术角度剖析其流程,确认其各个环节控制严格,无欺诈嫌疑, 最佳运气邀请码 : GZZKZK2, 你在注册时值得拥有, 无邀请码无奖励, 亲一定要记住.对 APP操作 ...

  8. [BTS] Correct the specified Action, or refer to the documentation on the allowed formats for the Actions

    A message sent to adapter "WCF-SAP" on send port "CNILG.iHouse.SAP.WCFSAP" with ...

  9. AT&T Assembly for Linux and Mac (sys_exit)

    Exit() in C : (sys_exit.c) int main(void) { ; } Exit() in AT&T for Linux: (sys_exit.s) .section ...

  10. jQuery实现Checkbox中项目开发全选全不选的使用

    <html> <head> <meta charset="utf-8"> <title>Checkbox的练习</title& ...