您的位置:首页 > 其它

HDU 5087 Revenge of LIS II(次大递增子序列)

2015-08-17 16:03 525 查看


Revenge of LIS II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1258 Accepted Submission(s): 423



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
HintFor 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

BestCoder Round #16

题目大意:求一个序列的所有递增子序列中第二长的那个递增子序列的长度。

解题思路:根据dp求解一个序列的最大递增子序列的模板,dp[i]表示以第i个数结尾的最长上升子序列的长度 ,c[i]表示到达dp[i]的方法数,比如序列1 2 2 3 2;dp[2]=2,c[2]=1;dp[3]=2,c[3]=1;dp[4]=3,c[4]=2。

最后查询最大递增子序列出现的次数,如果出现一次,就最大长度减一输出;如果出现不止一次,就输出最大长度。

代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
const int MAX=1005;
int a[MAX],c[MAX],dp[MAX];
int main()
{
int i,j,n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int ans=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
dp[i]=1,c[i]=1;
for(j=1;j<i;j++)
{
if(a[i]>a[j]&&dp[i]<dp[j]+1)
{
dp[i]=dp[j]+1;
c[i]=c[j];
}
else if(dp[i]==dp[j]+1)
c[i]=2;
}
ans=max(ans,dp[i]);
}
int num=0;
for(i=1;i<=n;i++)
{
if(dp[i]==ans)
num+=c[i];
}
if(num==1)
printf("%d\n",ans-1);
else
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: