hdu 5087(次长上升子序列)
Revenge of LIS II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1427 Accepted Submission(s): 489
computer science, the longest increasing subsequence problem is to find
a subsequence of a given sequence in which the subsequence's elements
are in sorted order, lowest to highest, and in which the subsequence is
as long as possible. This subsequence is not necessarily contiguous, or
unique.
---Wikipedia
Today, LIS takes revenge on you, again.
You mission is not calculating the length of longest increasing
subsequence, but the length of the second longest increasing
subsequence.
Two subsequence is different if and only they have
different length, or have at least one different element index in the
same place. And second longest increasing subsequence of sequence S
indicates the second largest one while sorting all the increasing
subsequences of S by its length.
Each
test case begins with an integer N, indicating the length of the
sequence. Then N integer Ai follows, indicating the sequence.
[Technical Specification]
1. 1 <= T <= 100
2. 2 <= N <= 1000
3. 1 <= Ai <= 1 000 000 000
2
1 1
4
1 2 3 4
5
1 1 2 2 2
3
2
For the first sequence, there are two increasing subsequence: [1], [1]. So the length of the second longest increasing subsequence is also 1, same with the length of LIS.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = ;
int dp[N],num[N];
int a[N];
int main(){
int tcase;
scanf("%d",&tcase);
while(tcase--){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
int ans = dp[] = num[]= ;
for(int i=;i<=n;i++){
dp[i] =num[i]= ;
for(int j=;j<i;j++){
if(a[i]>a[j]){
if(dp[i]==dp[j]+){
num[i]+=num[j];
}
if(dp[i]<dp[j]+){
dp[i] = dp[j]+;
num[i] = num[j];
}
}
}
ans = max(ans,dp[i]);
}
int cnt=;
for(int i=;i<=n;i++){
if(dp[i]==ans){
cnt+=num[i];
}
}
if(cnt==) printf("%d\n",ans-);
else printf("%d\n",ans);
}
return ;
}
hdu 5087(次长上升子序列)的更多相关文章
- hdu 5087 次长升序串的长度
http://acm.hdu.edu.cn/showproblem.php?pid=5087 求数列次长升序串的长度 还是dp求最长升序串的长度,再开一个数组记录对于dp值的串的个数 最后看一下最长字 ...
- hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)
链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...
- HDU 1231 最大连续子序列 --- 入门DP
HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...
- HDU 1231.最大连续子序列-dp+位置标记
最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...
- HDU 5087 (线性DP+次大LIS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目大意:求次大LIS的长度.注意两个长度相同的LIS大小比较,下标和大的LIS较大. 解题思 ...
- DP专题训练之HDU 1231 最大连续子序列
Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...
- HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)
C - 最大连续子序列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- hdu 5087 Revenge of LIS II
http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意求第二长的上升序列. 在求最长上升序列的同时加上一个数组,来记录以i为结尾的有多少条序列.如果n+1为 ...
随机推荐
- js和CSS3炫酷3D相册展示
<!doctype html> <html> <head> <meta charset="UTF"> <title>js ...
- 《Cracking the Coding Interview》——第17章:普通题——题目10
2014-04-28 23:54 题目:XML文件的冗余度很大,主要在于尖括号里的字段名.按照书上给定的方式进行压缩. 解法:这题我居然忘做了,只写了一句话的注解.用python能够相对方便地实现,因 ...
- mysql一机多实例安装记录
因为想研究mycat,所以需要安装多个mysql实例进行研究.限于没有多于计算机,只能在本机安装了.通过mysql文档,自己琢磨着安装成功! 目录结构如下: 其中one和two文件夹用来模拟数据库分库 ...
- Python全栈工程师(运算符、if)
ParisGabriel Python 入门基础 比较运算符:< 小于<= 小于等于> 大于>= 大于等于== 等于!= 不等于 语法: 表达式1>表达式 ...
- 不作伪分享者决定完整分享我自学Python3的全部过程细节
不作伪分享者决定完整分享我自学Python3的全部过程细节 我不要作伪分享者 十六年前我第一次见到了电脑,并深深地爱上了它: 十二年前我第一次连上了网络,并紧紧地被它爱上. 十年前的网络是田园美景 ...
- 【志银】Dev-Cpp配置OpenGL图形库(成功版本:Dev-Cpp 5.7.1 MinGW 4.8.1)
★配置前须知:Dev-Cpp自带OpenGL的使用和OpenGL简介 (附Dev-Cpp下载地址:http://sourceforge.net/projects/orwelldevcpp/?sourc ...
- JavaScript里面的正则以及eval
1.eval JavaScript中的eval是Python中eval和exec的合集,既可以编译代码也可以获取返回值. eval() EvalError 执行字符串中的JavaScript代码 ...
- Spring 学习笔记(五)—— Bean之间的关系、作用域、自动装配
继承 Spring提供了配置信息的继承机制,可以通过为<bean>元素指定parent值重用已有的<bean>元素的配置信息. <?xml version="1 ...
- Lambda表达式使用2
1.概述 本篇主要介绍lambda中常用的收集器,收集器的作用就是从数据流中生成需要的数据接口. 最常用的就是Collectors.toList(),只要将它传递给collect()函数,就能够使用它 ...
- ACM-Hero In Maze
Hero In Maze 时间限制(普通/Java):1000MS/10000MS 运行 ...