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

Problem Description
In
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.

 
Input
The first line contains a single integer T, indicating the number of test cases.

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

 
Output
For each test case, output the length of the second longest increasing subsequence.
 
Sample Input
3
2
1 1
4
1 2 3 4
5
1 1 2 2 2
 
Sample Output
1
3
2

Hint

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.

 
Source
 
题意:求解一个串中次长上升子序列的的长度。
题解:主要是求出有多少个最长上升子序列,如果个数大于1,那么次长就是最长,如果为1 ,那么次长就为最长的减一。关键是怎么统计最长上升子序列的个数??这里用到一个数组记录dp[i]的个数,当dp[j]+1>dp[i]时,dp[i]的个数为num[j],如果dp[j]+1==dp[i],那么 num[i] = num[i]+num[j]
#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(次长上升子序列)的更多相关文章

  1. hdu 5087 次长升序串的长度

    http://acm.hdu.edu.cn/showproblem.php?pid=5087 求数列次长升序串的长度 还是dp求最长升序串的长度,再开一个数组记录对于dp值的串的个数 最后看一下最长字 ...

  2. hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)

    链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...

  3. HDU 1231 最大连续子序列 --- 入门DP

    HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...

  4. HDU 1231.最大连续子序列-dp+位置标记

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...

  6. HDU 5087 (线性DP+次大LIS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目大意:求次大LIS的长度.注意两个长度相同的LIS大小比较,下标和大的LIS较大. 解题思 ...

  7. DP专题训练之HDU 1231 最大连续子序列

    Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...

  8. HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)

    C - 最大连续子序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  9. hdu 5087 Revenge of LIS II

    http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意求第二长的上升序列. 在求最长上升序列的同时加上一个数组,来记录以i为结尾的有多少条序列.如果n+1为 ...

随机推荐

  1. WPF系列教程——(三)使用Win10 Edge浏览器内核 - 简书

    原文:WPF系列教程--(三)使用Win10 Edge浏览器内核 - 简书 在需要显示一些 H5网站的时候自带的WebBrowser总是显示不了,WebBrowser使用的是IE内核,许多H5新特性都 ...

  2. 内部类inner class

    1.什么是内部类,为什么要用内部类? 可以将一个类定义在另一个类的定义内部,这就是内部类. 当描述事物时,事物的内部还有事物,该事物用内部类来描述,因为内部事物在使用外部事物的内容. 如: class ...

  3. [转]多多“亦”善:把大量内容放到一页PPT的5个技巧

    技巧一:利用灰色“隐蔽”内容 灰色有个好处:自动成为“备胎”,在“现任”被浏览后才会被注意到.所以使用灰色能够让页面内容看起来没那么多. 技巧二:对齐和亲密 这是排版的两个原则. 对齐是指对页面上的元 ...

  4. 剑指Offer - 九度1390 - 矩形覆盖

    剑指Offer - 九度1390 - 矩形覆盖2014-02-05 23:27 题目描述: 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形 ...

  5. 《Cracking the Coding Interview》——第12章:测试——题目2

    2014-04-24 23:15 题目:你有一段程序,运行了十次每次都在不同的地方崩掉了.已知这段程序只用了标准C或C++库函数,请问有什么思路来找出问题所在. 解法:1. 时间戳每次都不同.2. 随 ...

  6. 【Binary Search Tree Iterator 】cpp

    题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with th ...

  7. 社区版pycharm安装Django框架

    1.cmd下执行:pip3 install django 2.cmd下执行:django-admin startproject Demo (Demo为项目名称,可以更改你取的项目名称) 3.cmd下执 ...

  8. 1、python 循环控制

     案例1: lucky_num = 19 input_num = int(input("Input the guess number:")) if input_num == luc ...

  9. Python全栈工程师(装饰器、模块)

    ParisGabriel                每天坚持手写  一天一篇  决定坚持几年 全栈工程师     Python人工智能从入门到精通 装饰器 decorators(专业提高篇) 装饰 ...

  10. Android5.0新特性

    1.Activity转场动画 首先,把之前启动Activity的代码改成下面的写法: (如果低版本需要加注解@RequiresApi(api = Build.VERSION_CODES.LOLLIPO ...