博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU5087——Revenge of LIS II(BestCoder Round #16)
阅读量:5158 次
发布时间:2019-06-13

本文共 2803 字,大约阅读时间需要 9 分钟。

Revenge of LIS II

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.

题目大意:

    求第二长的绝对递增子序列的长度。

解题思路:

    错误思路:

        求出用于求最长绝对递增子序列的dp数组,sort之后输出dp[N-1]。

        未考虑到dp[N]可以有多种方式构成。eg:1 1 2 就应该输出2。

    正确思路:

        每次求dp[i]的时候,用c[i]记录有多少种情况来构成此最优解。

        求出ans=max(dp[1],dp[2]...dp[N]).

        在求出 sum=sum{ c[i] | dp[i]==ans }

        若sum!=1 说明最优解有多种可能的构成方式。输出ans即可。

        若sum==1 输出ans-1

Code:

1 /************************************************************************* 2     > File Name: BestCode#16_1002.cpp 3     > Author: Enumz 4     > Mail: 369372123@qq.com 5     > Created Time: 2014年11月01日 星期六 17时44分05秒 6  ************************************************************************/ 7  8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 #include
22 #define MAXN 500023 using namespace std;24 int dp[MAXN];25 long long a[MAXN];26 int flag[MAXN];27 int main()28 {29 int T;30 cin>>T;31 while (T--)32 {33 int N;34 cin>>N;35 for (int i=1;i<=N;i++){36 scanf("%I64d",&a[i]);37 dp[i]=1,flag[i]=1;38 }39 int ans=0;40 for (int i=1;i<=N;i++)41 {42 for (int j=1;j
1)56 printf("%d\n",ans);57 else58 printf("%d\n",ans-1);59 }60 return 0;61 }

 

转载于:https://www.cnblogs.com/Enumz/p/4068879.html

你可能感兴趣的文章
CF1106F Lunar New Year and a Recursive Sequence
查看>>
<iframe> 标签 中 src 的三种形式. display , echart
查看>>
MATLAB 程序计算结果出现 复数(a+bi)问题
查看>>
An Android APK is really a zip file
查看>>
Linux常用命令大全(转)
查看>>
poj 3669 Meteor Shower
查看>>
存储控制器使用【转】
查看>>
Spring浅谈
查看>>
使用路径arc-奥运五环
查看>>
Mybatis(三)返回值四.注解配置
查看>>
Robot Motion
查看>>
分布式设计
查看>>
[今日干货]一个吸粉效果也不错的APP
查看>>
bzoj1010: [HNOI2008]玩具装箱toy
查看>>
js 将json字符串转换为json对象或json对象转换成json字符串
查看>>
Rhino-- JavaScript
查看>>
Java考试笔记一
查看>>
DOM进行表格动态操作
查看>>
移植UE4的Spline与SplineMesh组件到Unity5
查看>>
leetcode 849. 到最近的人的最大距离(Maximize Distance to Closest Person)
查看>>