HDU 1950 Bridging signals (LIS,O(nlogn))
题意:
给一个数字序列,要求找到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代码
HDU 1950 Bridging signals (LIS,O(nlogn))的更多相关文章
- [POJ1631]Bridging signals (DP,二分优化)
题目链接:http://poj.org/problem?id=1631 就是求一个LIS,但是范围太大(n≤40000),无法用常规O(n²)的朴素DP算法,这时需要优化. 新加一个数组s[]来维护长 ...
- POJ 1631 Bridging signals(LIS 二分法 高速方法)
Language: Default Bridging signals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1076 ...
- (hdu)1950 Bridging signals(最长上升子序列)
Problem Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip f ...
- POJ 1631 Bridging signals(LIS的等价表述)
把左边固定,看右边,要求线不相交,编号满足单调性,其实是LIS的等价表述. (如果编号是乱的也可以把它有序化就像Uva 10635 Prince and Princess那样 O(nlogn) #in ...
- hdu 4507 数位dp(求和,求平方和)
http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...
- HDU 1074 Doing Homework (动态规划,位运算)
HDU 1074 Doing Homework (动态规划,位运算) Description Ignatius has just come back school from the 30th ACM/ ...
- HDU 3338 Kakuro Extension (网络流,最大流)
HDU 3338 Kakuro Extension (网络流,最大流) Description If you solved problem like this, forget it.Because y ...
- HDU 4280 Island Transport(网络流,最大流)
HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...
- HDU 6187 Destroy Walls (思维,最大生成树)
HDU 6187 Destroy Walls (思维,最大生成树) Destroy Walls *Time Limit: 8000/4000 MS (Java/Others) Memory Limit ...
随机推荐
- js联动
html: <!-- 省 --> <div class="col-sm-2"> <select name="p_id"> & ...
- seafile文档
https://manual-cn.seafile.com/deploy_windows/download_and_setup_seafile_windows_server.html
- Jquery获取web窗体关闭事件,排除刷新页面
在js脚本里全局定义一个 var r=true;若是刷新的话则把r=false; $(window).unload(function () { if (r) { //这里面证明用户不是点的F5刷新 执 ...
- window.showModalDialog 在谷歌Uncaught TypeError: undefined is not a function
if(navigator.userAgent.indexOf("Chrome") >0 ){var winOption = "height="+heigh ...
- MFC CMap整理
映射表类(CMap)是MFC集合类中的一个模板类,也称作为“字典”.CMap是把唯一关键码映射到值的字典收集类,使用CMap可以构造一个关键字和元素值映射的集合类.一旦在映射中插入了一个关键码值对(元 ...
- 525. Contiguous Array
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- 五分钟了解Mecanim角色动画系统
http://www.narkii.com/club/thread-305414-1.html Unity 4.0推出的Mecanim动画系统已经有一段时间,不过据了解很多的朋友仍然在使用原来的角色动 ...
- NDAP 日志
2014.04.29 1.理论债券价格CalculateExpetedBondPrice计算有误差 CalculateLibrary中的计算理论债券价格(计算理论期货价格的反函数)和正确结果有误差(可 ...
- cmder安装
官网地址:http://gooseberrycreative.com/cmder/ 一款非常漂亮好用的cmd工具. 在github或者官网下载后解压,点击Cmder.exe即可启动. 32位系统会遇到 ...
- [android][avd位置修改][需自己手动]
1.找到avd目录(C:\的那个)并打开,移动.avd文件到需要的位置. 2.修改ini文件path,完成.