分析:

给一个序列,求出每个位置结尾的最长上升子序列

O(n^2) 超时

#include "cstdio"
#include "algorithm"
#define N 1005
#define INF 0X3f3f3f3f
using namespace std;
int a[N];
int dp[N];
void solve(int n)
{
for(int i=;i<n;i++)
{
dp[i]=;
for(int j=;j<i;j++)///往前找寻美妙的回忆
{
if(a[j]<a[i])
{
dp[i]=std::max(dp[i],dp[j]+);
}
}
}
for(int i=;i<n;i++)
{
if(i==)
printf("%d",dp[]);
else
printf(" %d",dp[i]);
}
printf("\n");
} int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
solve(n);
}
}

优化为O(nlogn)  AC

#include "cstdio"
#define N 100005
#include "algorithm"
using namespace std;
int n;
int a[N];
int dp[N];
int ans[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&a[]);
int top=;///最长上升子序列长度
dp[]=a[];///=最后一个元素
ans[]=top;///每个位置的 最长上升..长度 for(int j=;j<n;j++)///对每个元素
{
scanf("%d",&a[j]);
if(a[j]>dp[top])///变长
{
top++;
dp[top]=a[j];
ans[j]=top;
}
else
{
int pos=lower_bound(dp,dp+top,a[j])-dp;///二分查找位置 替换元素
dp[pos]=a[j];
ans[j]=pos;
}
}
for(int i=;i<n;i++)
{
if(i==)
printf("%d",ans[i]);
else
printf(" %d",ans[i]);
}
printf("\n");
}
}

若只要最长...,只输出ans[n-1]

可将上述解法当做一模板

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int dp[];
const int inf=0x7fffffff;
int a[]={,,,,,,};
const int maxn=;
int main()
{
fill(dp,dp+,inf);
for(int i=;i<;i++)
{
*lower_bound(dp,dp+,a[i])=a[i];
}
int len=lower_bound(dp,dp+,inf)-dp;
for(int i=;i<len;i++)
cout<<dp[i]<<endl;
return ;
}

HDU5748---(记录每个元素的 最长上升子序列 nlogn)的更多相关文章

  1. HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...

  2. 【算法】最长公共子序列(nlogn)

    转载注明出处:http://blog.csdn.net/wdq347/article/details/9001005 (修正了一些错误,并自己重写了代码) 最长公共子序列(LCS)最常见的算法是时间复 ...

  3. 最长公共子序列 nlogn

    先来个板子 #include<bits/stdc++.h> using namespace std; , M = 1e6+, mod = 1e9+, inf = 1e9+; typedef ...

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

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

  5. DP练习 最长上升子序列nlogn解法

    openjudge 百练 2757:最长上升子序列 总时间限制:  2000ms 内存限制:  65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候, ...

  6. NYOJ 214 最长上升子序列nlogn

    普通的思路是O(n2)的复杂度,这个题的数据量太大,超时,这时候就得用nlogn的复杂度的算法来做,这个算法的主要思想是只保存有效的序列,即最大递增子序列,然后最后得到数组的长度就是最大子序列.比如序 ...

  7. hdu1950 最长上升子序列nlogn

    简单. #include<cstdio> #include<cstring> #include<iostream> using namespace std; ; i ...

  8. hdu1025 最长上升子序列 (nlogn)

    水,坑. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm&g ...

  9. 最长上升子序列 nlogn

    ; LL num[N]; LL dp[N]; LL go(LL l, LL r, LL k) { for (; r >= l; r--) if (dp[r] <= k) return r; ...

随机推荐

  1. C++各种类型的简单排序大汇总~

    啊,排序的技能点也太多了吧!!!LITTLESUN快要**在排序的技能场了啊!(划掉)经历了两天48小时2880分钟172800秒的艰苦奋斗,终于终于终于学的差不多了!明天就可以去打排序的小怪喽!(撒 ...

  2. hdu4742 Pinball Game 3D

    真他娘的搞不懂cdq分治的顺序问题.但是candy?的博客里提到过,多想想吧-- #include <algorithm> #include <iostream> #inclu ...

  3. Spring+quartz cron表达式(cron手册官方)完美理解

    ------------------------------------- 15 17/1 14/3 * * ? 从每小时的17分15秒开始 每分钟的15秒执行一次14:17:15 ...14:59: ...

  4. Java - 问题集 - linux下,jar: command not found

    linux下的找不到jar命令解决方法如下: 1. 确认jdk是否已安装 2. 检查jdk环境变量是否已设置,并且确认该设置已生效 3. 1,2两步均正常时,建立jar的软链接 # cd /usr/b ...

  5. thrift服务端到客户端开发简单示例

    (1)首先我们在服务器端写个helloworld.thrift文件,如下所示: service HelloWorld{ string ping(1: string name), string getp ...

  6. iOS-读写plist文件

    读写plist文件 问题,我有一个plist文件,表示56个民族的,但是里面保存的字典,我想转换成一个数组 好的,那么就先遍历这个plist,然后将结果保存到一个数组中,这里出现的一个问题就是C语言字 ...

  7. Python全栈 MongoDB 数据库(数据的查找)

      非关系型数据库和关系型数据库的区别? 不是以关系模型构建的,结构自由 非关系型数据库不保证数据一致性 非关系型数据库可以在处理高并发和海量数据时弥补关系数据库的不足 非关系型数据库在技术上没有关系 ...

  8. POJ 2229 递推

    Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T ...

  9. GBDT && XGBOOST

                                  GBDT && XGBOOST Outline Introduction GBDT Model XGBOOST Model ...

  10. android自定义View绘制圆形头像与椭圆头像

    要实现这两种效果,需要自定义View,并且有两种实现方式.   第一种: public class BitmapShaders extends View {     private  BitmapSh ...