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 ...
随机推荐
- Linux 无法登陆172.***.***.***的子网
1. sudo dhclient -r 这条命令重复执行几次 2. dhclient - 3.查看ifconfig
- ubuntu系统下挂载新的硬盘
参考 http://zwkufo.blog.163.com/blog/static/258825120141283942244/ http://blog.csdn.net/caicai2526 ...
- D - Bear and Finding Criminals
Description There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long ro ...
- E20180513-hm
letter n. 信; 证书,许可证; 字母,文字; 字面意义; bracket n. 支架,悬臂; 括弧; 类别,等级; [数] 同类项; coefficient n. 系数; (测定某种质量或 ...
- 洛谷 - P2281 - 多项式的加法和乘法 - 大模拟
题目链接:https://www.luogu.org/problemnew/show/P2281 题目的意思很简单,输入两个系数.指数都是整数,变量都是大写字母的多项式,求他们的加法结果和乘法结果. ...
- char*和CStringA互转
char*->CStringA char* ch1 = "中文测试123"; CStringA str(ch1); CStringA->char* char* ch2 ...
- C#利用WebService接口下载文件
WebTest.RtTfSimDataInterface test = new WebTest.RtTfSimDataInterface(); //string strBasic = test.Get ...
- Codeforces Round #374 (Div. 2)【A,B,C】
= =C题这种DP打的少吧,记得以前最短路分层图打过这样子的,然后比赛前半个小时才恍然大雾...然后瞎几把还打错了,还好A,B手速快..上分了: A题: 计算B的连续个数的组数,每组的连续个数: 水题 ...
- Telnet 命令格式
Telnet host 端口 如:Telnet 127.0.0.1 11211 执行命令进入后 ctr +] ,打开回显,并回车即可
- C++中的声明与定义
转自:http://www.cnblogs.com/kingcat/archive/2011/12/30/2307426.html C++是一种复杂的计算机语言,很多在现代高级语言中已经不需要程序员操 ...