HDU 5078 Revenge of LIS II(dp LIS)

本文介绍了一个计算机科学中的经典问题——寻找给定序列的第二长递增子序列。不同于最长递增子序列(LIS)问题,此问题需要找出所有递增子序列中长度次于最长的那个。文章提供了算法思路及实现代码,适用于算法竞赛和技术面试等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
 

Recommend

参考链接:http://blog.youkuaiyun.com/acvay/article/details/40686171


比赛时没有读懂题目开始做结果被hack了,后来一直想nlogn的方法,无果,以后应该会想出来,以后再贴那种方法代码


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
using namespace std;
#define N 10005

int a[N],dp[N],c[N];
int n;

int main()
{
	int i,j,t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(i=1;i<=n;i++)
			scanf("%d",&a[i]);

        int ans=0;
        for(i=1;i<=n;i++)
		{
			dp[i]=1;
			c[i]=1;
			for(j=1;j<i;j++)
			{
			   if(a[i]<=a[j]) continue;

			   if(dp[j]+1>dp[i])
			   {
			   	 dp[i]=dp[j]+1;
			   	 c[i]=c[j];
			   }
			   else
				if(dp[j]+1==dp[i])
				 c[i]=2;
			}
			if(dp[i]>ans)
				ans=dp[i];
		}
		j=0;
		for(i=1;i<=n;i++)
			if(dp[i]==ans)
		{
			j+=c[i];
			if(j>1)
				break;
		}
		if(j>1)
			printf("%d\n",ans);
		else
			printf("%d\n",ans-1);
	}
	return 0;
}




资源下载链接为: https://pan.quark.cn/s/9648a1f24758 Java JDK(Java Development Kit)是Java编程语言的核心组件,为开发和运行Java程序提供了必要的工具和环境。JDK 8是Oracle公司推出的一个重要版本,它引入了许多新特性和改进,极大地提升了开发效率和代码质量,对开发者来说具有极高的实用价值。 本次提供的“jdk-8u251-macosx-x64.dmg”安装包是专为Mac OS X系统设计的64位版本,其中不仅包含了Java运行环境(JRE),还涵盖了丰富的开发工具,方便用户在Mac平台上进行Java程序的开发与运行。 JDK 8的关键更新和特性如下: Lambda表达式:这是JDK 8的一项重大语法创新,允许开发者使用简洁的匿名函数替代复杂的多行回调代码,从而使代码更加简洁、易读且高效。 方法引用与构造器引用:与Lambda表达式配合使用,可以更直观地引用已有的方法或构造器,进一步减少冗余代码,提升代码的可维护性。 Stream API:这是一个用于处理集合的新API,采用声明式处理方式,使集合操作(如过滤、映射和归约等)更加高效且易于理解。 日期和时间API的改进:JDK 8对日期和时间API进行了重构,引入了java.time包,包含LocalDate、LocalTime和LocalDateTime等类,替代了原有的java.util.Date和java.util.Calendar,使日期和时间的处理更加友好和灵活。 Optional类:为解决null对象导致的空指针异常问题,JDK 8引入了Optional类,它是一个容器对象,可以表示一个值存在或不存在,从而有效避免空指针异常。 接口的默认方法和静态方法:接口现在可以定义默认方法(使用default关键字)和静态方法。默认方法允许在不破坏向后
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值