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. bitset学习

    bitset是个好东西.嗯.贼sao~ 很早就讲过但是我并没有弄懂.(现在也不敢说明白) 首先bitset是有常数的,而且常数并不能忽略不计——$(\frac{1}{32})$ 目前我也只是会bits ...

  2. 冒泡排序&快速排序

    1.排序分为以下四类共七种排序方法: 交换排序: 1) 冒泡排序  2) 快速排序 选择排序: 3) 直接选择排序  4) 堆排序 插入排序: 5) 直接插入排序  6) 希尔排序 合并排序: 7) ...

  3. Percona-Tookit工具包之pt-heartbeat

      Preface       Replication delay is a common issue in MySQL replications.Especially in those replic ...

  4. Jetty 安装、启动与项目部署

    Jetty是当下非常流行的一款轻量级Java Web服务器和Servlet容器实现,它由Eclipse基金会托管,完全免费而且开放源代码,因此所有人均可以从其官网下载最新源代码进行研究.由于其轻量.灵 ...

  5. mysql基础知识点整理

    数据库与数据表的创建.修改.删除 创建数据库: Create database 数据库名 删除数据库: drop database 数据库名 修改字符集为utf8并指定校对集(mysql默认字符集为l ...

  6. 使用 JsonPath 完成接口自动化测试中参数关联和数据验证(Python语言)

    背景: 接口自动化测试实现简单.成本较低.收益较高,越来越受到企业重视 restful风格的api设计大行其道 json成为主流的轻量级数据交换格式 痛点: 接口关联 也称为关联参数.在应用业务接口中 ...

  7. hnust 档案管理

    问题 E: 档案管理 时间限制: 1 Sec  内存限制: 128 MB提交: 274  解决: 105[提交][状态][讨论版] 题目描述 X老师管理着学校的档案室,经常会有其他的老师来档案室存文件 ...

  8. React跨域问题解决

    https://segmentfault.com/q/1010000012732581 非跨域问题报错 -rpccorsdomain="http://localhost:3000" ...

  9. hexo站点地图

    将网站链接提交到百度 百度搜索引擎提交入口 有三种验证方式,我选择Html标签验证,在themes\next\layout\_partials\head.swing中添加验证代码: <meta ...

  10. SheetJS & Error: Sheet names cannot exceed 31 chars

    SheetJS Error: Sheet names cannot exceed 31 chars title + version https://github.com/SheetJS/js-xlsx ...