题意:

  给一个数字序列,要求找到LIS,输出其长度。

思路:

  扫一遍+二分,复杂度O(nlogn),空间复杂度O(n)。

  具体方法:增加一个数组,用d[i]表示长度为 i 的递增子序列的最后一个元素,且该元素总是保持当前最小。初始化d[1]=A[i],当前LIS的长度len=1。从 2 to n,若A[i]>d[len],则d[++len]=A[i],否则,在数组d中找到A[i]应该插入的位置,代替掉那个第一个比它大的数字,比如d[k]<A[i]<=d[k+1],直接将A[i]代替掉d[k+1]。完成后len就是LIS的长度了。

 #include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 2147483647
#define LL unsigned long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
const int mod=1e9+;
int a[N], d[N];
int* lower_( int *s,int *e,int val ) //二分找值,返回下标
{
int L=, R=e-s-, mid;
while(L<R)
{
mid=R-(R-L+)/; //保证至少减少1
if( s[mid]<val ) L=mid+;//至少增加1
else R=mid;
}
return &s[R];
} int main()
{
freopen("input.txt", "r", stdin);
int t, n, len;
cin>>t;
while(t--)
{
scanf("%d",&n);
for(int i=; i<=n; i++) scanf("%d",&a[i]);
len=;
d[len]=a[]; for( int i=; i<=n; i++ )
{
if( a[i]>d[len] ) d[++len]=a[i];
else *lower_(d+,d+len+,a[i])=a[i];
//else *lower_bound(d+1,d+len+1,a[i])=a[i]; 上一行代码可换成此行
}
printf("%d\n",len);
}
return ;
}

AC代码

最新文章

  1. 使用Github Pages建独立博客
  2. C扩展Python - official docs - defining new type
  3. 动态 SQL
  4. selenium2.0处理case实例(一)
  5. TestNG扩展
  6. PHPSTORM实用快捷键
  7. python操作redis-zset
  8. Difference between LINQ to SQL and LINQ to Entity(DataContext and DbContext)
  9. SSAS系列&mdash;&mdash;【04】多维数据(物理体系结构)
  10. 兼容不同浏览器的 CSS Hack 写法
  11. iOS回顾笔记(07) -- UITableView的使用和性能优化
  12. Android Foreground Service (前台服务)
  13. PV &amp; PVC - 每天5分钟玩转 Docker 容器技术(150)
  14. oracle 关于对时间操作的汇总
  15. PHP 闭包函数
  16. 我在MySQL免安装版使用过程中遇到的问题记录【二】
  17. NorFlash 学习
  18. SpringBoot - 添加定时任务
  19. CH 1402 - 后缀数组 - [字符串hash]
  20. TensorRT下安装pycuda

热门文章

  1. mysql : mysql 5.6.13 免安装版配置
  2. Game of Peace
  3. C#操作句柄
  4. Lightoj 1147【DP】
  5. UGUI(四)事件系统的封装
  6. hdu2147(yy)
  7. [Xcode 实际操作]八、网络与多线程-(23)多线程的同步与异步的区别
  8. [Xcode 实际操作]九、实用进阶-(21)使用“调试视图”查看各界面元素的层次顺序
  9. Android近场通信---NFC基础(二)(转)
  10. js+canvas(H5)实现小球移动小demo