HDU5748---(记录每个元素的 最长上升子序列 nlogn)
分析:
给一个序列,求出每个位置结尾的最长上升子序列
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)的更多相关文章
- HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...
- 【算法】最长公共子序列(nlogn)
转载注明出处:http://blog.csdn.net/wdq347/article/details/9001005 (修正了一些错误,并自己重写了代码) 最长公共子序列(LCS)最常见的算法是时间复 ...
- 最长公共子序列 nlogn
先来个板子 #include<bits/stdc++.h> using namespace std; , M = 1e6+, mod = 1e9+, inf = 1e9+; typedef ...
- [poj 1533]最长上升子序列nlogn树状数组
题目链接:http://poj.org/problem?id=2533 其实这个题的数据范围n^2都可以过,只是为了练习一下nlogn的写法. 最长上升子序列的nlogn写法有两种,一种是变形的dp, ...
- DP练习 最长上升子序列nlogn解法
openjudge 百练 2757:最长上升子序列 总时间限制: 2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候, ...
- NYOJ 214 最长上升子序列nlogn
普通的思路是O(n2)的复杂度,这个题的数据量太大,超时,这时候就得用nlogn的复杂度的算法来做,这个算法的主要思想是只保存有效的序列,即最大递增子序列,然后最后得到数组的长度就是最大子序列.比如序 ...
- hdu1950 最长上升子序列nlogn
简单. #include<cstdio> #include<cstring> #include<iostream> using namespace std; ; i ...
- hdu1025 最长上升子序列 (nlogn)
水,坑. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm&g ...
- 最长上升子序列 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; ...
随机推荐
- LeetCode:11. ContainerWithWater(Medium)
原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an ...
- Thymeleaf 常用th标签基础整理
(一)Thymeleaf 是个什么? 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下 ...
- 创龙TMS320C6748开发找不到 tl.dsp.evm6748的问题研究
1. 使用中遇到问题,看了一下帖子说是把tl.dsp.evm6748换成ti.platforms.evm6748可以编译过去.这个包是在XDCtools里面的. js: "D:/ti/ccs ...
- c++ combination by next_permutation
#include <iostream> #include <algorithm> #include <vector> int main() { int n, r; ...
- Hackerrank - [Algo] Matrix Rotation
https://www.hackerrank.com/challenges/matrix-rotation-algo 又是一道耗了两小时以上的题,做完了才想起来,这不就是几年前在POJ上做过的一个同类 ...
- 『Golang』MongoDB在Golang中的使用(mgo包)
有关在Golang中使用mho进行MongoDB操作的最简单的例子.
- 发布npm包 登录报错 E409 Conflict
1.到官网注册个账号,并且验证完邮箱:https://www.npmjs.com/ 2.打开cmd命令行 登录:$npm login 根据提示 一步步完成登录. 3.新建一个项目文件夹: npmtes ...
- MySQL高可用之PXC安装部署(续)
Preface Yesterday I implemented a three-nodes PXC,but there were some errors when proceeding ...
- 【性能调优】一次关于慢查询及FGC频繁的调优经历
以下来分享一个关于MySQL数据库慢查询和FGC频繁的性能案例. 一.系统架构 一个简单的dubbo服务,服务提供者提供接口,并且提供接口的实现,提供方注册服务到Zookeeper注册中心,然后消费者 ...
- 剑指offer-旋转数组的最小数字06
题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...